我得到的JSON错误的含义

时间:2014-04-04 06:03:34

标签: android json

我是android编程的新手。帮助我解释错误。我正在使用的Web服务是否有任何问题。我在登录和注册页面使用Web服务。

 04-04 01:45:09.939: W/System.err(1463): org.json.JSONException: Value yes of type java.lang.String cannot be converted to JSONObject
    04-04 01:45:09.979: W/System.err(1463):     at org.json.JSON.typeMismatch(JSON.java:111)
    04-04 01:45:09.979: W/System.err(1463):     at org.json.JSONObject.<init>(JSONObject.java:159)
    04-04 01:45:09.979: W/System.err(1463):     at org.json.JSONObject.<init>(JSONObject.java:172)
    04-04 01:45:09.979: W/System.err(1463):     at com.sunmobileappnow.mobileappnow.LoginActivity$SigIn.doInBackground(LoginActivity.java:145)
    04-04 01:45:09.979: W/System.err(1463):     at com.sunmobileappnow.mobileappnow.LoginActivity$SigIn.doInBackground(LoginActivity.java:1)
    04-04 01:45:09.979: W/System.err(1463):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
    04-04 01:45:09.999: W/System.err(1463):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    04-04 01:45:09.999: W/System.err(1463):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    04-04 01:45:09.999: W/System.err(1463):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    04-04 01:45:09.999: W/System.err(1463):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    04-04 01:45:10.009: W/System.err(1463):     at java.lang.Thread.run(Thread.java:841)

这是我的代码: -

private class SigIn extends AsyncTask<String, String, String[]> {
        ProgressDialog pDialog = new ProgressDialog(LoginActivity.this);
        @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 signing in...");
                    runOnUiThread(new Runnable() 
                    {
                        public void run() 
                        {
                            pDialog.show();
                        }
                    });

                    JSONObject job= new JSONObject();
                    euid = uedit.getText().toString();
                    epass = pedit.getText().toString();

                    euid.replace("" ,"%20");
                    epass.replace("" ,"%20");


                    job.put("status_key","2");
                    job.put("method", "login");
                    job.put("email", euid);
                    job.put("password",epass );

                    StringEntity se = new StringEntity(job.toString());

                    HttpPost httppost = new HttpPost("http://suntechwebsolutions.com/clients/mobileapp_now/webservice.php");

                    httppost.setEntity(se);

                    HttpResponse response = httpclient.execute(httppost);

                    String data = EntityUtils.toString(response.getEntity());

                    System.out.println("response "+data);

                    JSONObject jo = new JSONObject(data);
                    JSONArray jArray = jo.getJSONArray("user_details");

                    for (int i=0; i < jArray.length(); i++)
                    {
                        System.out.println("please print my name...for....");
                        try {


                                JSONObject jsob = jArray.getJSONObject(i);
                                // Pulling items from the array
                                //uid = jsob.getString("id");
                                mfname = jsob.getString("first_name");
                                msname = jsob.getString("surname");
                                mun = jsob.getString("unit_no");
                                mstreet = jsob.getString("street_name");
                                msuburb = jsob.getString("suburb");
                                mstate = jsob.getString("state");
                                mpassword = jsob.getString("password");
                                mpostcode = jsob.getString("post_code");
                                mlicense = jsob.getString("driving_license");
                                mid = job.getString("authority_id");
                                mmobile = jsob.getString("mobile_no");
                                memail = jsob.getString("email");


                               System.out.println(mpassword+".....please print my name......."+mpassword);
                               System.out.println(uid+"......please print my name......."+mfname);
                               System.out.println(mstate +"......please print my name......."+mlicense);
                               System.out.println(mmobile+"........please print my customertype......."+memail);

                         } catch (JSONException e) {

                        } 
                    }
                            if(jo.getString("err-code").equals("0"))
                    {
                        final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this);
                        alert.setTitle("Login!!!");
                        alert.setMessage(jo.getString("message"));
                        alert.setPositiveButton("Ok",
                                new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) 
                            {
                                pDialog.dismiss();


                                dialog.dismiss();

                                Intent tabint = new Intent(getApplicationContext(),MainActivity.class);


                                startActivity(tabint);
                        }
                        });
                        runOnUiThread(new Runnable() {
                            public void run() {
                                alert.show();
                            }
                        });
                    }
                    else
                    {

                        final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.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;
        }

登录和注册页面的JSON代码: -

function login($data) 
{
    $email = mysql_real_escape_string($data -> email);

    $password = mysql_real_escape_string($data -> password);

    $query = mysql_query("SELECT * from driver_register where Email='$email' and password=md5('$password')");

    if($row = mysql_fetch_assoc($query)) 
    {
        $token = mysql_real_escape_string(generateRandomString(150));

        mysql_query("Update driver_register set login_token='$token' where id='{$row['id']}'");
        if (mysql_affected_rows() > 0) 
        {   
            return array("message" => "Login successful.", "token"=>$token,"err-code" => "0");
        }
        else 
        {
            return array("message" => "Server is under progress.", "err-code" => "500", "error" => mysql_error());
        }


    }
    else 
    {
        return array("message" => "Username and Password does not match. Kindly try again.", "err-code" => "300", "err" => mysql_error() . $sql);
    }
}


function driver_register($data)
{
    $fname      = mysql_real_escape_string($data->first_name);
    $password       = mysql_real_escape_string($data->password);
    $sname      = mysql_real_escape_string($data->surname);
    $unit       = mysql_real_escape_string($data->unit_no);
    $street     = mysql_real_escape_string($data->street_name);
    $suburb     = mysql_real_escape_string($data->suburb);
    $state      = mysql_real_escape_string($data->state);
    $post_code  = mysql_real_escape_string($data->post_code);
    $dr_lic     = mysql_real_escape_string($data->driving_license);
    $authority_id  = mysql_real_escape_string($data->authority_id);
    $mobile_no  = mysql_real_escape_string($data->mobile_no);
    $email      = mysql_real_escape_string($data->email);

    $lic        = $data -> license_pic;
    $audit      = $data -> audit_card_pic;
    $face   = $data -> face_pic;

    $sql = mysql_query("SELECT * FROM driver_register where Email='$email'");
    $ans = mysql_fetch_assoc($sql);
    if(empty($ans))
    {

        $sql="INSERT INTO `driver_register`(`password`,`first_name`, `Surname`, `unit_no`, `street_name`, `Suburb`, `State`, `post_code`, `driving_lic`, `authority_id`, `mobile_no`, `Email`, `date_of_creation`) VALUES (md5('$password'),'$fname','$sname','$unit','$street','$suburb','$state','$post_code','$dr_lic','$authority_id','$mobile_no','$email',NOW())";
        //echo $sql;
        mysql_query($sql);

        if (mysql_affected_rows() > 0) {
                    $image_lic = base64_decode($lic);
                    $image_audit = base64_decode($audit);
                    $image_face = base64_decode($face);

                    $i_id = mysql_insert_id();

                    file_put_contents("upload/license_". $i_id . ".png", $image_lic);
                    $path_driver = mysql_real_escape_string("upload/license_" . $i_id . ".png");

                    file_put_contents("upload/audit_" . $i_id . ".png", $image_audit);
                    $path_audit = mysql_real_escape_string("upload/audit_" . $i_id . ".png");

                    file_put_contents("upload/face_" . $i_id . ".png", $image_face);
                    $path_face = mysql_real_escape_string("upload/face_" . $i_id . ".png");

                    mysql_query("UPDATE `driver_register` SET `license_pic`='$path_driver',`audit_card_pic`='$path_audit',`face_pic`='$path_face' WHERE `id`='$i_id'");

                }


        if(mysql_affected_rows()>0)
        {
            return array("err-code"=>"0","message"=>"Registration Successfully");   
        }
        else
        {
            return array("err-code"=>"404","message"=>"Registration not successfull");
        }
    }
    else
    {
        return array("err-code"=>"1","message"=>"Email-Id Already Exist");
    }
}

从响应中删除yes后: -

04-04 04:13:54.219: D/response(2034): {
04-04 04:13:54.219: D/response(2034):     "message": "Login successful.",
04-04 04:13:54.219: D/response(2034):     "token": "cE5J09FgZCxlyWLcJpHik1ttBcP02p8e4eY4oElngSJPOu1yUIQeKjIlvxlyXuM1IL66pruGkdv9IxHCgyR0Rzln7HV4cI6Utc1TEwzYK58tCP5TnXTfwfDEXzI9hO4L16EFDeEnjMQWCWP0TJgqZT",
04-04 04:13:54.219: D/response(2034):     "err-code": "0"
04-04 04:13:54.219: D/response(2034): }
04-04 04:13:54.229: W/System.err(2034): org.json.JSONException: No value for user_details
04-04 04:13:54.229: W/System.err(2034):     at org.json.JSONObject.get(JSONObject.java:355)
04-04 04:13:54.229: W/System.err(2034):     at org.json.JSONObject.getJSONArray(JSONObject.java:549)
04-04 04:13:54.239: W/System.err(2034):     at com.sunmobileappnow.mobileappnow.LoginActivity$SigIn.doInBackground(LoginActivity.java:150)
04-04 04:13:54.239: W/System.err(2034):     at com.sunmobileappnow.mobileappnow.LoginActivity$SigIn.doInBackground(LoginActivity.java:1)
04-04 04:13:54.239: W/System.err(2034):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-04 04:13:54.249: W/System.err(2034):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-04 04:13:54.249: W/System.err(2034):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-04 04:13:54.249: W/System.err(2034):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-04 04:13:54.259: W/System.err(2034):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-04 04:13:54.259: W/System.err(2034):     at java.lang.Thread.run(Thread.java:841)

4 个答案:

答案 0 :(得分:1)

错误消息表示您尝试将JSON字符串值作为JSON对象访问。

答案 1 :(得分:0)

请发布您的代码和json回复, 在这种情况下,您可以尝试将String转换为jsonObject。

JSONObject jsonObject=new JSONObject(responseWeb);// responseWeb is String variable and you can convert string  to JsonObject like this.

答案 2 :(得分:0)

如果要将字符串转换为JSONObject,则需要执行以下操作:

JSONObject obj = new JSONObject("{a string}");

更新: data的外观如何?

JSONObject jo = new JSONObject(data);

也许您需要添加大括号:

JSONObject jo = new JSONObject("{" + data + "}");

答案 3 :(得分:0)

您正试图在代码中的某处将JSONString转换为JSONObject:

com.sunmobileappnow.mobileappnow.LoginActivity $ SigIn.doInBackground(LoginActivity.java:145)

或者你可以使用JSONTokener:

Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
 //you have an object
else if (json instanceof JSONArray)
 //you have an array