无法将java.lang.String类型的值yes转换为JSONObject
我有一个注册页面,我正试图把数据放在服务器上。但是我得到了JSONException。有人能告诉我为什么我会得到这个错误吗。我只是把编辑文本值带到服务器上 代码:-无法将java.lang.String类型的值yes转换为JSONObject,java,android,json,android-asynctask,Java,Android,Json,Android Asynctask,我有一个注册页面,我正试图把数据放在服务器上。但是我得到了JSONException。有人能告诉我为什么我会得到这个错误吗。我只是把编辑文本值带到服务器上 代码:- private class DriverRegistration extends AsyncTask<String, String, String[]> { ProgressDialog pDialog = new ProgressDialog(Registration.this);
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);
}
}
私有类驱动器注册扩展异步任务{
ProgressDialog pDialog=新的ProgressDialog(Registration.this);
@TargetApi(构建版本代码姜饼)
@凌驾
受保护字符串[]doInBackground(最终字符串…参数)
{
ConnectivityManager conMgr=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
如果(conMgr.getActiveNetworkInfo()!=null
&&conMgr.getActiveNetworkInfo().isAvailable()
&&conMgr.getActiveNetworkInfo().isConnected())
{
HttpClient HttpClient=新的DefaultHttpClient();
尝试
{
pDialog.setMessage(“请稍候驱动器注册…”);
runOnUiThread(新的Runnable()
{
公开募捐
{
pDialog.show();
}
});
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
JSONObject作业=新建JSONObject();
mfname=fname.getText().toString();
msname=sname.getText().toString();
mun=un.getText().toString();
mstreet=street.getText().toString();
msuburb=suburban.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.替换(“,“%20”);
mstreet.替换(“,“%20”);
msuburb.替换(“,“%20”);
mstate.替换(“,“%20”);
mpostcode.replace(“,“%20”);
M许可证。替换(“,“%20”);
中间替换(“,“%20”);
移动。替换(“,“%20”);
成员。替换(“,“%20”);
作业放置(“方法”、“驱动程序/寄存器”);
job.put(“名字”,mfname);
job.put(“姓氏”,msname);
作业。put(“单元号”,mun);
job.put(“街道名称”,mstreet);
job.put(“郊区”,城市郊区);
job.put(“state”,mstate);
job.put(“post_code”、“mpostcode”);
工作。put(“驾驶执照”,mlicense);
作业。put(“权限id”,mid);
job.put(“mobile_no”,mmobile);
job.put(“email”,memail);
StringEntity se=新的StringEntity(job.toString());
HttpPost HttpPost=新的HttpPost(“http://example.com/webservice.php");
httppost.setEntity(se);
HttpResponse response=httpclient.execute(httppost);
字符串数据=EntityUtils.toString(response.getEntity());
System.out.println(“响应”+数据);
字符串调用;
呼叫=数据;
System.out.println(“打印我………”+调用);
JSONObject jo=新的JSONObject(数据);
if(jo.getString(“err code”).equals(“0”))
{
final AlertDialog.Builder alert=new AlertDialog.Builder(Registration.this);
alert.setTitle(“alert!!!”);
setMessage(jo.getString(“message”);
alert.setPositiveButton(“确定”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int(按钮)
{
pDialog.disclose();
dialog.dismise();
startActivity(新意图(注册.this,MainActivity.class));
}
});
runOnUiThread(新的Runnable(){
公开募捐{
alert.show();
}
});
}
其他的
{
final AlertDialog.Builder alert=new AlertDialog.Builder(Registration.this);
警报。设置标题(“警报!”);
setMessage(jo.getString(“message”);
alert.setPositiveButton(“确定”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int(按钮){
dialog.dismise();
}
});
runOnUiThread(新的Runnable()
{
公开募捐
{
pDialog.disclose();
alert.show();
}
});
}
}
捕获(例外e)
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)