JSONArray错误获取

时间:2014-06-24 09:41:57

标签: java android json arrays jsonobject

我有登录系统,在获得响应或进程请求生成后,我得到NullPointerException。我的登录请求是:

try {
               if (json.getString(KEY_SUCCESS) != null) {

                    String res = json.getString(KEY_SUCCESS);

                    if(res == "sucess"){
                        pDialog.setMessage("Loading User Space");
                        pDialog.setTitle("Getting Data");
                        UserFunctions logout = new UserFunctions();
                        logout.logoutUser(getApplicationContext());
                        Intent upanel = new Intent(getApplicationContext(), Main.class);
                        upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        pDialog.dismiss();
                        startActivity(upanel);

                        finish();
                    }else{

                        pDialog.dismiss();
                        loginErrorMsg.setText("Incorrect username/password");
                    }
                }
            } 

登录建设是:

public JSONArray loginUser(String email, String password, String appkey)  {

    String conc = email + password + appkey;

    JSONArray json = jsonParser.getJSONFromUrl(loginURL + "?login=" + email 
    + "&password=" + password + "&sign=" + conc);

    return json;
}

在jsonParser中我有这段代码:

public class JSONParser {

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

    // constructor
    public JSONParser() {

    }
public JSONArray getJSONFromUrl(String url) {

        // Making HTTP request

            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

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


            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();

        // try parse the string to a JSON object

            jObj = new JSONArray(json);
        // return JSON String
        return jObj;
    }
}

顺便说一下JSON响应的类型:

{ "status": "success", 
"message": "", 
"session_id": "asdasddfcvxgdgfdfv", 
   "user": 
          [{ "company": "company", 
          "last_name": "last_name·", 
          "name": "name", 
          "middle_name": "middle_name", 
          "phone": "+1234567890", 
          "photo": "avatar.png" }] }

在此操作之后,我得到“null值”错误,如下所示:

Error converting result java.lang.NullPointerException: lock == null
Error parsing data org.json.JSONException: End of input at character 0 of

2 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

public JSONObject getJSONFromUrl(String url) {


 try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpclient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                httpEntity.getContent(), HTTP.UTF_8));

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

        } finally {
            reader.close();
        }
            jObj = new JSONObject(reader.toString());
            return jObj;
     } catch (MalformedURLException localMalformedURLException) {
        return null;
     } catch (IOException localIOException) {
        return null;
     }catch (Exception e){
        return null;
     }
}

答案 1 :(得分:0)

您的回复是JsonObject,您正在将其解析为JSONArray

了解更多信息。 http://json.org/