我遇到了一个问题,有效的 JSON 字符串不能成为 JSON 对象。
当我从浏览器调用我的 url 时,它会返回有效的 JSON 字符串。请检查
public void initializeHttpClient() { httpclient = new DefaultHttpClient(); nameValuePairs = new ArrayList(2); }
public JSONObject sendHttpRequest(String url) {
try {
postRequest = new HttpPost(url);
postRequest.setHeader("Content-type", "application/json");
postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
httpResponse = httpclient.execute(postRequest);
httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String responseString = EntityUtils.toString(httpEntity);
// 这里问题正在发生..
JSONObject responseObject = new JSONObject(responseString);
return responseObject;
}
} catch (Exception e) {
e.getMessage();
}
return null;
}
public JSONObject getLogin(String serviceUrl,String o_email, String o_password,String o_user_id, String o_network_type, String o_format) {
initializeHttpClient();
if(serviceUrl!=null){
nameValuePairs.add(new BasicNameValuePair("login_email",o_email));
nameValuePairs.add(new BasicNameValuePair("password",o_password));
nameValuePairs.add(new BasicNameValuePair("user_id",o_user_id));
nameValuePairs.add(new BasicNameValuePair("network_type",o_network_type));
nameValuePairs.add(new BasicNameValuePair("format",o_format));
return sendHttpRequest(serviceUrl);
}
return null;
}