Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取JSONArray错误_Java_Android_Json_Arrays_Jsonobject - Fatal编程技术网

Java 获取JSONArray错误

Java 获取JSONArray错误,java,android,json,arrays,jsonobject,Java,Android,Json,Arrays,Jsonobject,我有一个登录系统,在得到响应或进程内请求生成后,我会得到NullPointerException。我的登录请求是: try { if (json.getString(KEY_SUCCESS) != null) { String res = json.getString(KEY_SUCCESS); if(res == "sucess"){

我有一个登录系统,在得到响应或进程内请求生成后,我会得到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" }] }
执行此操作后,我得到如下“空值”错误:

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


更多信息

我认为你的json对象不是空的,它的一些地方是空的。所以你会得到异常。可能你会得到
null
result。我需要得到“status”:“success”或数组“user”你试图得到json数组,我得到用户json请更改getJSONFromUrl(字符串url)将JsonArray返回给Jsonobject。我在转换结果java.lang.NullPointerException:lock==null和org.json.jsoneException:i get:E/WindowManager的字符0处的输入结束时出错﹕ android.view.WindowLeaked:Activity com.rieltclub.app.LoginActivity已经泄漏了window com.android.internal.policy.impl.PhoneWindow$DecorView{41f0c6d8 V.E…..R…D 0,0-684324},最初添加在这里的:json=userFunction.logiuser(电子邮件、密码、密钥);res=json.getString(KEY_SUCCESS);