1

I am getting file not found exception with 405 status code when I try to pass Authorization Bearer using Asynctask and if I comment the line passing Authorization Bearer I am getting 403 with status code.

below is my code.

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);
                }
            }

Any help will be appreciated! Thanks in Advance!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
Sunil P
  • 3,698
  • 3
  • 13
  • 20
  • `AsyncTask` is not related to `Authorization` `HttpURLConnection` is . have a look at [This](https://stackoverflow.com/a/15555952/4168607) it may help . – ADM Mar 07 '18 at 07:58
  • so what does it mean can't we do it from `Asynctask` @ADM – Sunil P Mar 07 '18 at 08:56

0 Answers0