[英]Java value of type java.lang.string cannot be converted to jsonobject
[英]Value yes of type java.lang.String cannot be converted to JSONObject
我有一個注冊頁面,我正在嘗試將數據放在服務器上。 但是我正在獲取JSONException。有人可以告訴我為什么我得到這個錯誤嗎? 我還只是將編輯文本值帶到服務器上。
碼:-
private class DriverRegistration extends AsyncTask<String, String, String[]> {
ProgressDialog pDialog = new ProgressDialog(Registration.this);
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected String[] doInBackground(final String... params)
{
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
{
HttpClient httpclient = new DefaultHttpClient();
try
{
pDialog.setMessage("Please wait DriverRegistration...");
runOnUiThread(new Runnable()
{
public void run()
{
pDialog.show();
}
});
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
JSONObject job= new JSONObject();
mfname = fname.getText().toString();
msname = sname.getText().toString();
mun= un.getText().toString();
mstreet = street.getText().toString();
msuburb = suburb.getText().toString();
mstate= state.getText().toString();
mpostcode = postcode.getText().toString();
mlicense =license.getText().toString();
mid = id.getText().toString();
mmobile = mobile.getText().toString();
memail= email.getText().toString();
mfname.replace("" ,"%20");
msname.replace("" ,"%20");
mun.replace("" ,"%20");
mstreet.replace("" ,"%20");
msuburb.replace("" ,"%20");
mstate.replace("" ,"%20");
mpostcode.replace("" ,"%20");
mlicense.replace("" ,"%20");
mid.replace("" ,"%20");
mmobile.replace("" ,"%20");
memail.replace("" ,"%20");
job.put("method","driver_register");
job.put("first_name",mfname);
job.put("surname",msname);
job.put("unit_no",mun);
job.put("street_name",mstreet);
job.put("suburb",msuburb);
job.put("state",mstate);
job.put("post_code","mpostcode");
job.put("driving_license",mlicense);
job.put("authority_id",mid);
job.put("mobile_no",mmobile);
job.put("email",memail);
StringEntity se = new StringEntity(job.toString());
HttpPost httppost = new HttpPost("http://example.com/webservice.php");
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity());
System.out.println("response "+data);
String call;
call = data;
System.out.println("print me............."+call);
JSONObject jo = new JSONObject(data);
if(jo.getString("err-code").equals("0"))
{
final AlertDialog.Builder alert = new AlertDialog.Builder(Registration.this);
alert.setTitle("Alert!!!");
alert.setMessage(jo.getString("message"));
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
pDialog.dismiss();
dialog.dismiss();
startActivity(new Intent(Registration.this,MainActivity.class));
}
});
runOnUiThread(new Runnable() {
public void run() {
alert.show();
}
});
}
else
{
final AlertDialog.Builder alert = new AlertDialog.Builder(Registration.this);
alert.setTitle("Alert !");
alert.setMessage(jo.getString("message"));
alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
}
});
runOnUiThread(new Runnable()
{
public void run()
{
pDialog.dismiss();
alert.show();
}
});
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return params;
}
@Override
protected void onPostExecute(String[] result)
{
super.onPostExecute(result);
}
}
}
錯誤:-
04-03 01:02:13.731: I/System.out(1476): response yes {"err-code":"0","message":"Registration Successfully","Status_key":"2"}
04-03 01:02:13.731: I/System.out(1476): print me.............yes {"err-code":"0","message":"Registration Successfully","Status_key":"2"}
04-03 01:02:13.741: W/System.err(1476): org.json.JSONException: Value yes of type java.lang.String cannot be converted to JSONObject
04-03 01:02:13.741: W/System.err(1476): at org.json.JSON.typeMismatch(JSON.java:111)
04-03 01:02:13.751: W/System.err(1476): at org.json.JSONObject.<init>(JSONObject.java:159)
04-03 01:02:13.751: W/System.err(1476): at org.json.JSONObject.<init>(JSONObject.java:172)
04-03 01:02:13.751: W/System.err(1476): at com.sunmobileappnow.mobileappnow.Registration$DriverRegistration.doInBackground(Registration.java:345)
04-03 01:02:13.751: W/System.err(1476): at com.sunmobileappnow.mobileappnow.Registration$DriverRegistration.doInBackground(Registration.java:1)
04-03 01:02:13.751: W/System.err(1476): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-03 01:02:13.751: W/System.err(1476): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-03 01:02:13.751: W/System.err(1476): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-03 01:02:13.751: W/System.err(1476): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-03 01:02:13.751: W/System.err(1476): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-03 01:02:13.751: W/System.err(1476): at java.lang.Thread.run(Thread.java:841)
如您所見, data
變量不是合法的Json。 它以字符串“ yes”開頭,后跟json對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.