我正在调用 Web 服务以获取 JSON 字符串响应,它包含不在原始字符串中的反斜杠。下面是我请求一个 JSON 字符串对象的代码,它是: {"name":"Name","id":1}
protected String doInBackground(String... uri)
{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
Run Code Online (Sandbox Code Playgroud)
在执行后,我只是尝试将此响应字符串解析为 JSONObject。代码如下:
protected …Run Code Online (Sandbox Code Playgroud)