2014-12-05 210 views
-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 在此先感谢。

+0

你对此有何回应?你有没有尝试把响应放在http://json.parser.online.fr/beta/ – 2014-12-05 07:44:32

+0

请显示'httpClient.execute(httpPost,responseHandler)' – 2014-12-05 07:45:43

+0

的响应,我认为你的服务是本地的,因此无法从broswer得到响应从上面url – KOTIOS 2014-12-05 07:48:13

回答

0

试试下面的代码:

Object json = new JSONTokener(response).nextValue(); 
if (json instanceof JSONArray) { 
JSONArray jsonArray = (JSONArray) json; 
// your logic 
}else if(json instanceof JSONObject){ 
//your logic 
} 
相关问题