简体   繁体   中英

Value yes of type java.lang.String cannot be converted to JSONObject

I have a registration page and I am trying to put the data on the server . But I am getting JSONException .can anybody please tell me why I am getting this error. I am only taking edit text values to the server yet .

code:-

    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);
        }
    }   

}

Error:-

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)

As you can see, the data variable isn't a legal Json. It starts with the string "yes", followed by the json object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM