本文介绍了如何解析来自401状态code JSON响应? Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚最近参与的Andr​​oid我有这个code,我有注释的一些信息。

I just got recently involved with android I have this code, I have it commented for some info.

 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响应code是 401 它抛出一个 IOException异常 java.io.IOException异常:未找到验证挑战,我不能让我的回应

this works fine if I supply correct username and password but if not the http response code is 401 it throws an IOException java.io.IOException: No authentication challenges found and I couldn't get my response

这是我预期的反应,当我得到401

this is my expected response when i get 401

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

任何人都可以帮助我,我有这样的任务,整天和我似乎无法完成。任何意见或建议,会怎么做。在此先感谢

could anyone help me I have this task whole day and i couldn't seem to finish. any comment or suggestion would do. Thanks in advance

推荐答案

con.getInputStream()只如果使用HTTP返回200和299之间的code。对于其他任何响应code使用con.getErrorStream( )。

con.getInputStream() works only if Http returns a code between 200 and 299. For any other response code use con.getErrorStream().

您可以看起来应该是这样:

You could should look like this:

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

这篇关于如何解析来自401状态code JSON响应? Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:08