获取JSONArray错误



我有登录系统,并且在获得响应或在进程请求生成后获得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

试试这个方法,希望能帮助你解决你的问题

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;
     }
}

您的响应是JsonObject,您正在将其解析为JSONArray

查看更多信息。http://json.org/

最新更新