android ไม่สามารถแปลง String เป็น JSONObject ได้ Logcat แจ้งว่า JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
ผมทำการส่ง String เข้า SQLServer ผ่าน PHP ข้อมูลเข้าครบทุกอย่างแต่แอปมันปิดตัวลงทันทีที่กดส่ง
คือใน Log แจ้งมาว่าอย่างนี้ครับ
JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
Code (Android-Java)
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String clickItem = (String) getListAdapter().getItem(position);
strClickItem = clickItem;
Log.d("Voice", "Choose = " + strClickItem);
ChooseVoice();
}
private void ChooseVoice() {
// TODO Auto-generated method stub
String[] info = new String[3];
if (strClickItem.equals("ผัดกระเพรา 50 บาท")){
String ID = "1";
String List = "Padkapao";
String Price = "50";
info[0] = ID;
info[1] = List;
info[2] = Price;
Log.d("Item ", info[0]+" "+info[1]+" "+info[2]);
new CreateNewProduct().execute(info);
} else {
if (strClickItem.equals("แกงเผ็ด 120 บาท")) {
String ID = "2";
String List = "Kangped";
String Price = "120";
info[0] = ID;
info[1] = List;
info[2] = Price;
Log.d("Item ", info[0]+" "+info[1]+" "+info[2]);
new CreateNewProduct().execute(info);
} else {
if (strClickItem.equals("แกงส้ม 150 บาท")) {
String ID = "3";
String List = "Kangsom";
String Price = "150";
info[0] = ID;
info[1] = List;
info[2] = Price;
Log.d("Item ", info[0]+" "+info[1]+" "+info[2]);
new CreateNewProduct().execute(info);
} else {
}
}
}
}
public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] list;
public MyPerformanceArrayAdapter(Activity context, String[] list) {
super(context, R.layout.activity_main,list);
this.context = context;
this.list = list;
}
public View getView(int Position,View converview,ViewGroup parent){
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.activity_main, null, true);
TextView Name = (TextView)rowView.findViewById(R.id.text);
//ImageView Image = (ImageView)rowView.findViewById(R.id.image);
String ListName =this.list[Position];
Name.setText(ListName);
/* if (ListName.startsWith("Elepan"))
Image.setImageResource(R.drawable.zoo1);
else if (ListName.startsWith("Fish"))
Image.setImageResource(R.drawable.zoo2);
else if (ListName.startsWith("Dog"))
Image.setImageResource(R.drawable.zoo3); */
return rowView;
}
}
class CreateNewProduct extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
String args_Id = params[0];
String args_List = params[1];
String args_Price = params[2];
Log.v("myTag", "Asych ID: "+args_Id+" "+args_Price+" "+args_List);
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("name", args_Id));
params1.add(new BasicNameValuePair("price", args_Price));
params1.add(new BasicNameValuePair("description", args_List));
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params1);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
//successfully created product
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
//closing this screen
//finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
Tag : Mobile, Android
Date :
2014-11-03 19:48:06
By :
ymir.mw
View :
1356
Reply :
1
Error นี้น่าจะเกิดจาก JSON ที่ส่งมามันผิด Format น่ะครับ และอีกอย่างลองใช้พวก try {.....} ในการดักจับ Error ครับ
Date :
2014-11-04 12:53:46
By :
mr.win
Load balance : Server 01