[英]java.lang.String cannot be converted to JSONObject
@Override
protected JSONObject doInBackground(String... params) {
String path = null;
String response = null;
HashMap<String, String> request = null;
JSONObject requestJson = null;
DefaultHttpClient httpClient = null;
HttpPost httpPost = null;
StringEntity requestString = null;
ResponseHandler<String> responseHandler = null;
// get the username and password
Log.i("Email", params[0]);
Log.i("Password", params[1]);
try {
path = "http://192.xxx.x.xxx/xxxxService/UsersService.svc/UserAuthentication";
new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
// set the API request
request = new HashMap<String, String>();
request.put(new String("Email"), params[0]);
request.put(new String("Password"), params[1]);
request.entrySet().iterator();
// Store locations in JSON
requestJson = new JSONObject(request);
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(path);
requestString = new StringEntity(requestJson.toString());
// sets the post request as the resulting string
httpPost.setEntity(requestString);
httpPost.setHeader("Content-type", "application/json");
// Handles the response
responseHandler = new BasicResponseHandler();
response = httpClient.execute(httpPost, responseHandler);
responseJson = new JSONObject(response);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
responseJson = new JSONObject(response);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return responseJson;
}
I'm using this code to login to my app.我正在使用此代码登录我的应用程序。
responseJson = new JSONObject(response);
I'm getting "success" for the response, but responseJson value is "null"我的响应“成功”,但 responseJson 值为“null”
This is what I got:这是我得到的:
Error converting result org.json.JSONException: Value Fail of type java.lang.String cannot be converted to JSONObject
I t will be good if i can get a salution Thanks in advance.如果我能得到问候,那就太好了,提前致谢。
Try below code:试试下面的代码:
Object json = new JSONTokener(response).nextValue();
if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
// your logic
}else if(json instanceof JSONObject){
//your logic
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.