字符串无法转换为JSON对象

时间:2014-08-04 12:15:01

标签: android android-json android-parser

当我从本地数据库获取数据时,我在Json对象中获取值并将其转换为String,结果可以在本地服务器中看到。

但是..当我在实时数据库上执行相同的代码时,数据无法转换为Json对象。

这是我的代码 -

    class Createoffer extends AsyncTask<String, String, String> {

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
            }

            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(
                        "http://10.0.2.2/Upshot_Loyalty_Program/android_api_retailer/create_offer.php");
                try {
                    session = new SessionManager(getApplicationContext());
                    HashMap<String, String> user = session.getUserDetails();
                    bus_id = user.get(SessionManager.KEY_B_ID);
                    emp_id = user.get(SessionManager.KEY_E_ID);
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            2);
                    nameValuePairs.add(new BasicNameValuePair("bus_id", bus_id));
                    nameValuePairs.add(new BasicNameValuePair("offer_name",
                            offer_name_final));
                    nameValuePairs.add(new BasicNameValuePair("offer_discount",
                            offer_discount));
                    nameValuePairs.add(new BasicNameValuePair("date",
                            offer_valid_till));
                    nameValuePairs.add(new BasicNameValuePair("start_time",
                            offer_starting_time));
                    nameValuePairs.add(new BasicNameValuePair("end_time",
                            offer_ending_time));
                    nameValuePairs.add(new BasicNameValuePair("redeem_times",
                            offer_times_redeem));
                    nameValuePairs
                            .add(new BasicNameValuePair("created_by", emp_id));
                    nameValuePairs.add(new BasicNameValuePair("offer_created_for",
                            offer_created_for1));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    response.getStatusLine().getStatusCode();
                    HttpEntity getResponseEntity = response.getEntity();
                    responseString = EntityUtils.toString(getResponseEntity);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }
                return responseString;
            }

            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                try {
                    JSONObject json = new JSONObject(responseString);
                    JSONArray jArray = json.getJSONArray("customer");
                    for (int i = 0; i < jArray.length(); i++) {
                        jsonobj = jArray.getJSONObject(i);
                        success = jsonobj.getString("success");
                        System.out.println(success);
                    }
                    if (success.equals("1")) {
                        Intent innv = new Intent(getApplicationContext(),
                                Offers.class);
                        Editor editor = sharedpreferences.edit();
                        editor.clear();
                        editor.commit();
                        startActivity(innv);
                        Toast toast = Toast.makeText(getApplicationContext(),
                                "Offer sent successful to " + total_eli_cus
                                        + " customers ", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } else {
                        Toast toast = Toast.makeText(getApplicationContext(),
                                "Offer Not Created", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

这是我在logcat中得到的错误 -

        08-04 17:34:59.724: W/System.err(2649): org.json.JSONException: Value You of type java.lang.String cannot be converted to JSONObject
        08-04 17:34:59.724: W/System.err(2649):  at org.json.JSON.typeMismatch(JSON.java:111)
        08-04 17:34:59.729: W/System.err(2649):  at org.json.JSONObject.<init>(JSONObject.java:158)
        08-04 17:34:59.729: W/System.err(2649):  at org.json.JSONObject.<init>(JSONObject.java:171)
        08-04 17:34:59.739: W/System.err(2649):  at com.pack.upshot.Offer_success$Createoffer.onPostExecute(Offer_success.java:152)
        08-04 17:34:59.739: W/System.err(2649):  at com.pack.upshot.Offer_success$Createoffer.onPostExecute(Offer_success.java:1)
        08-04 17:34:59.739: W/System.err(2649):  at android.os.AsyncTask.finish(AsyncTask.java:602)
        08-04 17:34:59.750: W/System.err(2649):  at android.os.AsyncTask.access$600(AsyncTask.java:156)
        08-04 17:34:59.750: W/System.err(2649):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
        08-04 17:34:59.760: W/System.err(2649):  at android.os.Handler.dispatchMessage(Handler.java:99)
        08-04 17:34:59.760: W/System.err(2649):  at android.os.Looper.loop(Looper.java:137)
        08-04 17:34:59.769: W/System.err(2649):  at android.app.ActivityThread.main(ActivityThread.java:4340)
        08-04 17:34:59.769: W/System.err(2649):  at java.lang.reflect.Method.invokeNative(Native Method)
        08-04 17:34:59.789: W/System.err(2649):  at java.lang.reflect.Method.invoke(Method.java:511)
        08-04 17:34:59.789: W/System.err(2649):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        08-04 17:34:59.789: W/System.err(2649):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        08-04 17:34:59.789: W/System.err(2649):  at dalvik.system.NativeStart.main(Native Method)

这是我在JSON中的结果 -

{
    "customer": [
        {
            "success": "1"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您的网络服务未创建有效的JSONJSON字符串只能以{[开头。 你的开头是String“数组”。

您可以在其维基百科条目here中阅读JSON格式。