嗨,我有时会收到此JsonException ...为什么发生这种情况,任何机构都可以在下面的代码中解决此问题,这是我的代码。

  public int getResult(String name, double lat, double lon) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            String query = URLEncoder.encode(name, "utf-8");

            HttpPost httppost = new HttpPost("http://00004rl.rcomhost.com/demo/?json=get_dealing&dev=1&store_name=" + query + "&longitude=" + lon
                    + "&latitude=" + lat);
            JSONObject object = new JSONObject();
            StringEntity se = new StringEntity(object.toString());
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httppost.setEntity(se);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            JSONObject jso = new JSONObject(sb.toString());
            if (jso.getString("status").equals("ok")) {
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }


我在异常以下,这是我的logcat在下面的时间

   09-25 19:13:26.079: W/System.err(4842): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
   09-25 19:13:26.079: W/System.err(4842):  at org.json.JSON.typeMismatch(JSON.java:107)
   09-25 19:13:26.079: W/System.err(4842):  at org.json.JSONObject.<init>(JSONObject.java:158)
   09-25 19:13:26.079: W/System.err(4842):  at org.json.JSONObject.<init>(JSONObject.java:171)
   09-25 19:13:26.079: W/System.err(4842):  at com.eheuristics.android.diegodeals.googleplacesandmaps.MainActivity$LoadPlaces.getResult(MainActivity.java:246)
   09-25 19:13:26.079: W/System.err(4842):  at com.eheuristics.android.diegodeals.googleplacesandmaps.MainActivity$LoadPlaces.doInBackground(MainActivity.java:208)
   09-25 19:13:26.079: W/System.err(4842):  at com.eheuristics.android.diegodeals.googleplacesandmaps.MainActivity$LoadPlaces.doInBackground(MainActivity.java:1)
   09-25 19:13:26.079: W/System.err(4842):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
   09-25 19:13:26.079: W/System.err(4842):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
  09-25 19:13:26.089: W/System.err(4842):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
   09-25 19:13:26.089: W/System.err(4842):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
   09-25 19:13:26.089: W/System.err(4842):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
  09-25 19:13:26.089: W/System.err(4842):   at java.lang.Thread.run(Thread.java:1019)

最佳答案

您的服务器有时会以html而不是json的形式向您发送响应。

08-18 18:09