2015-12-30 114 views
1

我试图解析JSON,我能够通过Log.e("JSON Object", String.valueOf(json));接收回应{"status":false,"code":"101","message":"Cannot find a POST request in register"}之后,我得到一个JSONException: No value for user。请任何人都帮我解决这个问题。 和我已经检查了互联网和其他问题的教程在stackoverflow。但仍然无法解决我的问题。不能显示JSON数组,JSONException:无值错误

我的JSON响应

{"status":false,"code":"101","message":"Cannot find a POST request in register"} 

logcat的错误

W/System.err: org.json.JSONException: No value for user 

我的JSON响应解析代码:

 //URL to get JSON Array 
     private static String url = "http://mywebsite/api/index.php?action=user_register"; 

     //JSON Node Names 
     private static final String TAG_USER = "user"; 
     private static final String TAG_ID = "status"; 
     private static final String TAG_NAME = "code"; 
     private static final String TAG_EMAIL = "message"; 
     JSONArray user = null; 

    // Creating new JSON Parser 
     JSONParser jParser = new JSONParser(); 

     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 

try { 
      // Getting JSON Array 
      user = json.getJSONArray(TAG_USER); 
      JSONObject c = user.getJSONObject(0); 

      // Storing JSON item in a Variable 
      String id = c.getString(TAG_ID); 
      String name = c.getString(TAG_NAME); 
      String email = c.getString(TAG_EMAIL); 

      Toast.makeText(getContext(), id , Toast.LENGTH_SHORT).show(); 
      Toast.makeText(getContext(), name , Toast.LENGTH_SHORT).show(); 
      Toast.makeText(getContext(), email , Toast.LENGTH_SHORT).show(); 
    } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 

JSONParser.java

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    public JSONObject getJSONFromUrl(String url) { 

     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
} 
+0

没有'你的反应中user'标签。 – RobinHood

+1

尝试使用 if(json.has(TAG_USER)) user = json.getJSONArray(TAG_USER); – Madhur

回答

2

尝试使用

if(json.has(TAG_USER)) 
user = json.getJSONArray(TAG_USER); 

因此,如果没有这样的标记ü不会得到错误

已更新:

// Storing JSON item in a Variable 
      String id = json.getString(TAG_ID); 
      String name = json.getString(TAG_NAME); 
      String email = json.getString(TAG_EMAIL); 
+0

我试过了,但它显示java.lang.NullPointerException:尝试调用虚拟方法'org.json.JSONObject org.json.JSONArray.getJSONObject(int)'在空对象引用 –

+1

检查更新后的ans,根据你的json有没有jsonarray,你正在得到一个json对象作为回应 – Madhur

+0

感谢它的工作 –

1

这是因为您的回复中没有user标记,并且您试图获取相同的标记。

您的回应:

{"status":false,"code":"101","message":"Cannot find a POST request in register"} 

和你正在做的:user = json.getJSONArray(TAG_USER);,这里user标签丢失其抛出错误。

W/System.err的:org.json.JSONException:用户

没有价值
+0

所以。我将如何解决这个错误。我需要显示状态,代码和消息。你能帮我吗? –

1

你是不是在你的回应越来越user

{"status":false,"code":"101","message":"Cannot find a POST request in register"} 

也就是说你正在JSONException: No value Error

尝试用这种

if(json.has(TAG_USER)) 
{ 
    user = json.getJSONArray(TAG_USER); 
    JSONObject c = user.getJSONObject(0); 

    // Storing JSON item in a Variable 
    String id = c.getString(TAG_ID); 
    String name = c.getString(TAG_NAME); 
    String email = c.getString(TAG_EMAIL); 


} 

它将为user取值,如果它是可用的原因在你的回应中

+0

空指针异常 –

+0

在哪里?哪一行? –

1

代替使用'getJSONMethods'使用'optJSONMethods'如果有机会丢失标签。

'optMethods'不会抛出异常。它根据类型返回一些默认值。 JSONArray类型为空。

对于你的情况,你可以使用:

user = json.optJSONArray(TAG_USER); 

if(null != user) { 
    JSONObject c = user.optJSONObject(0); 

    // Storing JSON item in a Variable 
    if(null != c) { 
     String id = c.optString(TAG_ID); 
     String name = c.optString(TAG_NAME); 
     String email = c.optString(TAG_EMAIL); 
    } 
} 
+0

得到空指针异常 –