android - 如何解析来自 401 状态码的 json 响应?安卓

标签 android

我最近才接触到 android 我有这段代码,我已经对它进行了评论以获取一些信息。

 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: No发现身份验证挑战,但我无法得到响应

这是我收到 401 时的预期响应

{
    "status": "error",
    "message": "",
    "data": [],
    "status_code": 402
}

谁能帮助我,我整天都在做这个任务,但我似乎无法完成。任何意见或建议都可以。提前致谢

最佳答案

con.getInputStream() 仅在 Http 返回 200 到 299 之间的代码时有效。对于任何其他响应代码,请使用 con.getErrorStream()。

你应该看起来像这样:

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

关于android - 如何解析来自 401 状态码的 json 响应?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30712079/

相关文章:

android - logcat 停止写入设备上的文件

android - Kotlin,安卓平台。如何检查文本是否包含任何数字/符号/字符?

android - 调用 AudioRecord.start() 时出现 IllegalStateException

java - 为什么我在尝试从 Android 访问 Gmail 时收到 javax.mail.NoSuchProviderException?

android - 从服务 Android M 请求位置权限

android - 应用程序被杀死后 ViewPager 出现 NullPointerException

android - 在 Cordova 应用程序中使用 cordova-plugin-firebase-analytics 插件在 Android 上出现无效的 google_app_id 错误

android - 尝试使用 Canvas 在其上绘制文本时图像显示为黑色

android - 三星 Galaxy S III 上的 CSS @media 查询

android - 在 Android 中构建 Webkit