gpt4 book ai didi

java - 随机获取org.json.JSONException : Value <!类型为java.lang.String的DOCTYPE无法转换为JSONObject的错误

转载 作者:行者123 更新时间:2023-12-02 00:12:08 28 4
gpt4 key购买 nike

嗨,我有时会收到此 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。

关于java - 随机获取org.json.JSONException : Value &lt;!类型为java.lang.String的DOCTYPE无法转换为JSONObject的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12584914/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com