java - 类型为 java.lang.String 的值 yes 无法转换为 JSONObject

标签 java android json android-asynctask

我有一个注册页面,我正在尝试将数据放在服务器上。但是我得到了 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/

相关文章:

java - 使用 FK 作为过滤器在 Java 上创建查询

java - 如何制作SeekBar分段,并且每个分段显示不同的颜色?

java - 使用 GSON 解析类

javascript - AngularJS,使用 Ng-Repeat 和 Ng-Factory 显示 Json 数据

java - 如何实现与 UI 无关的后台工作任务

java - 如何将原始文件夹中的音频文件共享到社交应用程序?

java - android java从内部类访问,需要声明为final

android - 如何创建带动画的 TextSwitcher

python - 将 Pandas 系列导出为 JSON - numpy 类型错误

ios - 使用字典解析 JSON 数组