【发布时间】:2014-09-23 11:17:04
【问题描述】:
我使用上述服务从服务器获取响应并将其解码为字符串生成器。但是当我运行该应用程序时,它会显示警告
org.json.JSONException: java.lang.String 类型的值 无法转换为 JSONArray
在日志猫中。
服务器端没有问题。
protected String doInBackground(String... params) {
StringBuilder stringBuilder = new StringBuilder();
try{
HttpResponse response = null;
HttpParams httpParameters = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(httpParameters);
JSONObject jobj = new JSONObject();
try {
jobj.put("page_id",pageID);
} catch (JSONException e) {
e.printStackTrace();
}
String url = "http://myservice.com/gustbook_berry/mobile/GetOrder";
Log.i("Send URL:", url);
// HttpGet request = new HttpGet(url);
HttpPost request = new HttpPost(url);
List<NameValuePair> page = new ArrayList<NameValuePair>(1);
page.add(new BasicNameValuePair("page_id", jobj.toString()));
Log.d(TAG, url + page);
request.setEntity(new UrlEncodedFormEntity(page));
response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
//String output=EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
//System.out.println("OUT PUT->>"+output);
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
System.out.println(stringBuilder.toString()+"\n");
}
}catch(Exception e){
System.out.println(" error ocurres :" + e.getMessage());
}
return stringBuilder.toString();
}
请帮忙
【问题讨论】:
-
记录响应并检查它是否使用任何字符集或算法进行编码
-
没有它没有编码。它的痛苦 json 文本
-
BOM,BOM,BOM,BOM ...
-
确保您的字符串是 JSON 文本...
-
那些字符是 UTF-8 BOM。请参阅stackoverflow.com/questions/4614378/… 了解如何禁用它