使用Asynctask设置授权承载?

时间:2018-03-07 07:49:33

标签: android android-asynctask authorization bearer-token

当我尝试使用Asynctask传递授权承载时,我正在获取405状态代码的文件未找到异常,并且如果我评论通过授权承载的线路,我将获得403状态代码。

下面是我的代码。

class getloantypes extends AsyncTask<String, String,String>{
                private String LOANURL= "http://185.78.163.41:8080/paychec/loan/type";


                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                }

                @Override
                protected String doInBackground(String... strings) {
                    try{

                        /*URL url = new URL("http://185.78.163.41:8080/paychec/loan/type");
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                        conn.setReadTimeout(10000);
                        conn.setConnectTimeout(15000);
                        conn.setRequestMethod("GET");
                        conn.setDoInput(true);

                        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                        conn.setRequestProperty("Authorization", "Bearer " +  new MyPreferences(AddLoansActivtiy.this).getTokenKey());*/
                        URL url = new URL(LOANURL);

                        String verfiyresponse;

                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                        conn.setRequestMethod("GET");
                        conn.setDoOutput(true);
                        conn.setDoInput(true);
                        conn.setRequestProperty("Content-Type",
                                "application/json");

                        conn.setRequestProperty("Authorization", "Bearer "+new MyPreferences(AddLoansActivtiy.this).getTokenKey());
                        verfiyresponse = conn.getResponseMessage();
                        Log.e("resverfiy", verfiyresponse);
                        int status = conn.getResponseCode();
                        Log.d("statuscode", ""+status);
                        BufferedReader in = new BufferedReader(
                                new InputStreamReader(conn.getInputStream()));
                        String inputLine;
                        StringBuffer response = new StringBuffer();

                        while ((inputLine = in.readLine()) != null) {
                            response.append(inputLine);
                        }
                        in.close();

                        //print result
                        System.out.println(response.toString());
                        Log.d("jsonres", response.toString());
                        conn.connect();



                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);
                }
            }

任何帮助将不胜感激!提前致谢!

0 个答案:

没有答案