-3
@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;
}
我正在使用此代碼登錄到我的應用程序。java.lang.String無法轉換爲JSONObject
responseJson = new JSONObject(response);
我得到的響應「成功」,但responseJson值是「空」
這是我得到:
Error converting result org.json.JSONException: Value Fail of type java.lang.String cannot be converted to JSONObject
我T將如果我能好得到一個salution 在此先感謝。
你對此有何迴應?你有沒有嘗試把響應放在http://json.parser.online.fr/beta/ – 2014-12-05 07:44:32
請顯示'httpClient.execute(httpPost,responseHandler)' – 2014-12-05 07:45:43
的響應,我認爲你的服務是本地的,因此無法從broswer得到響應從上面url – KOTIOS 2014-12-05 07:48:13