I 웹 사이트에서 JSON 문자열을 가져 오는 코드가 있습니다된 JSONObject는 안드로이드 4.0 구문 분석 있지만 이전 버전에
{"status":"0","user_id":"123","name":"test","session_code":"6f33d4ee610651530f04b1f700ebc36d"}
:
post.setEntity(entity);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
result = EntityUtils.toString(resEntity, HTTP.UTF_8);
이 결과에서 JSON의 예입니다
은 jsonlint.com에서 유효성을 검사합니다. 나는 안드로이드 4 일
JSONObject jo = new JSONObject(result);
를 사용하여 분석을 시도, 코드는 잘 작동하지만 이전 버전에, 그것은, 내가 열심히 대신 온라인으로 얻을 수있는 JSON 문자열을 코딩
org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
를 제공합니다 이전 버전에서 작업했습니다. http 요청 출력을 로깅했을 때 모든 성공적인 요청마다 변경되는 세션 코드를 제외하고는 모든 것이 동일하다는 것을 알았지 만 두 세션 코드의 길이와 형식이 같았습니다.
누구에게이 이유가 무엇이며 어떻게 수정해야하는지 알고 있습니다.
UPDATE :
if (!email.isEmpty() && !password.isEmpty()){
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://www.xxxxxx.com/api/login.php";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("email", email));
postParams.add(new BasicNameValuePair("pass", password));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams, HTTP.UTF_8);
post.setEntity(entity);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
result = EntityUtils.toString(resEntity, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, e.toString());
} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
} catch (ParseException e) {
Log.e(TAG, e.toString());
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
if (result != null) {
try {
JSONObject jo = new JSONObject(result);
switch (jo.getInt("status")) {
case 0 : {
userName = jo.getString("name");
userID = jo.getInt("user_id");
authCode = jo.getString("session_code");
return 0;
}
default:
return jo.getInt("status");
}
} catch (JSONException e) {
Log.e(TAG, e.toString());
}
}
는 대신으로
JSONObject
을 반환해야합니다 JSONObject. – gowri을 위해 당신을 공유 .. 안드로이드 2.3.3 버전 자체에서 안드로이드에 코드를 JSON을 사용할 수 있습니다 – Agata
이 결과를 시도 = EntityUtils.toString (resEntity); – Sumant