我有一个注册页面,我正在尝试将数据放在服务器上。但是我正在获取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对象。
关于java - 类型为java.lang.String的值yes无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22828373/