0

I have protected some hosted files with a password to avoid any unknow access but I don't know how to directly access these files from my application , I am using Volley library and I tried this :

    private static final String URL = "http://user:password@mywebsite.com/file/json/";

public static void getData(final Context context) {

    requestQueue = Volley.newRequestQueue(context.getApplicationContext());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                String somedata= response.getString("data");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
          //  Toast.makeText(context, "Error Loading Data\n Please Restart App", Toast.LENGTH_SHORT).show();
        }
    });
    requestQueue.add(jsonObjectRequest);
    }

I realised that my app doesn't fetch data from my json file , but when I access the URL above from browser , it works perfectly

Chaouki Anass
  • 937
  • 1
  • 10
  • 19

1 Answers1

0

Do not put your password into Url. Volley supported all Http methods. You should use Post or Put methods

new JsonObjectRequest(Request.Method.POST, URL, null, new Response.Listener<JSONObject>(){
 ...
}

It seems your URL is incorrect or you are using redirection. Because of the result code, chrome auto calls a new url. Apart of these browser automatically set the port 80 if port is not defined. Also pay attention on your back end side

see Android volley to handle redirect

Vahe Gharibyan
  • 5,277
  • 4
  • 36
  • 47
  • URL has a specific structure host://domain:port/path?query_params. Instead of port number you had passed password@mywebsite.com – Vahe Gharibyan Dec 23 '17 at 00:26