Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.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
Android 如何从401状态代码解析json响应?安卓_Android - Fatal编程技术网

Android 如何从401状态代码解析json响应?安卓

Android 如何从401状态代码解析json响应?安卓,android,Android,我最近刚加入安卓系统,我有这段代码,我对它发表了一些评论 HttpURLConnection con = null; String token = null; String message = null; try { con = (HttpURLConnection)new URL(Constants.URL_LOGIN +

我最近刚加入安卓系统,我有这段代码,我对它发表了一些评论

 HttpURLConnection con = null;
                    String token = null;
                    String message = null;
                    try {


                        con = (HttpURLConnection)new URL(Constants.URL_LOGIN + deviceID).openConnection();
                        con.setDoOutput(true);
                        con.setUseCaches(false);
                        con.setRequestMethod("POST");
                        con.setRequestProperty("Accept", "application/json");

                        DataOutputStream dos = new DataOutputStream(con.getOutputStream());

                        System.out.println("pin wala" + username + password + pincode);
                        dos.write((Constants.JSON_LOGIN_USERNAME + "=" + username + "&" + Constants.JSON_LOGIN_PASSWORD + "=" + password).getBytes(Charset.forName("UTF-8")));


                        dos.close();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                        StringBuilder jsonStringBuilder = new StringBuilder();
                        String s = null;
                        while((s = reader.readLine()) != null) {
                            jsonStringBuilder.append(s);
                            System.out.println(s);
                        }

                        reader.close();

                        JSONObject jsonLogin = new JSONObject(jsonStringBuilder.toString());

                        if(jsonLogin != null) {
                                //here, should go the the return

                                //if statuscode is 401 i have this mystatus to be setted if not the api call is successful

                            if(jsonLogin.getInt("mystatus") == 402){

                                MainActivity.this.forPincode = true;
                            } else {

                                // no problem here this is successful api call 
                            }

                        }


                    } catch(MalformedURLException e){
                        e.printStackTrace();
                    } catch (IOException e) {

                        e.printStackTrace();
                    } catch(JSONException e){
                        e.printStackTrace();
                    }
                    finally {
                        if(con != null)
                            con.disconnect();
                    }
如果我提供了正确的用户名和密码,这可以正常工作,但如果没有,http响应代码是
401
,它会抛出
IOException
java.io.IOException:未找到身份验证问题
,并且我无法获得响应

这是我拿到401后的预期反应

{
    "status": "error",
    "message": "",
    "data": [],
    "status_code": 402
}
有人能帮我吗?我整天都在做这件事,但我似乎无法完成。任何意见或建议都可以。提前感谢。getInputStream()仅在Http返回介于200和299之间的代码时才起作用。对于任何其他响应代码,请使用con.getErrorStream()

你可以这样看:

BufferedReader reader = new BufferedReader(new InputStreamReader(con.getResponseCode() / 100 == 2 ? con.getInputStream() : con.getErrorStream()));

但是如果我使用getErrorStream,我应该能够得到200个返回?我编辑了响应。基本上,您将对2xx响应使用getInputStream(),对其他所有响应使用getErrorStream()。仍然获得
java.io.IOException:未发现任何身份验证挑战
看起来这是第二个问题-您应该仍然使用我提供的代码,否则任何代码>=300的响应都会失败。对于您的I/O异常,请检查此@Azertii hi,该链接修复了
未发现身份验证挑战的问题。另外,使用上面的代码读取错误流的代码也可以正常工作。
getInt(“mystatus”)==402.1
你确定吗?@njzk2嗨,我编辑我的帖子是为了直接讨论如果我得到401,会得到响应的问题。但我不确定,因为我没走那么远。因此,我无法获取我的响应代码。但谢谢你指出这一点。