2014-12-05 10 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 

я т будет хорошо, если я могу получите заботу Спасибо заранее.

+0

Каков ваш ответ? Вы пытались ответить на ответ в http://json.parser.online.fr/beta/ –

+0

Просьба показать ответ от 'httpClient.execute (httpPost, responseHandler)' –

+0

. Я думаю, что служба ur является локальной, поэтому не может получить ответ от broswer от выше url – KOTIOS

ответ

0

Try ниже код:

Object json = new JSONTokener(response).nextValue(); 
if (json instanceof JSONArray) { 
JSONArray jsonArray = (JSONArray) json; 
// your logic 
}else if(json instanceof JSONObject){ 
//your logic 
} 
Смежные вопросы