0

Doubt Similar to Time out error volley

I am implementing a login Signup Screen using Localhost The Button which implements the login has the onClick Listener as

b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("lala","1");
         request=new StringRequest(Request.Method.POST, loginURL, new Response.Listener<String>() {
             @Override
             public void onResponse(String response) {
                 Log.d("lala","3");
                 try {
                     JSONObject jsonObject=new JSONObject(response);
                     boolean sucess=jsonObject.getBoolean("sucess");
                     if(sucess)
                     {
                         Toast.makeText(getApplicationContext(),jsonObject.getString("username").toString()+" Welcome",Toast.LENGTH_LONG).show();
                         Log.d("lala","4");
                     }
                     else
                     {
                         Toast.makeText(getApplicationContext()," Failed ",Toast.LENGTH_LONG).show();
                     }
                 } catch (JSONException e) {
                     Log.d("lala","5");
                     e.printStackTrace();
                 }
             }
         }, new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError error) {
                 Log.d("lala","2");
                 Log.e("YOUR_APP_LOG_TAG", "I got an error", error);

             }
         }){
             @Override
             protected Map<String, String> getParams() throws AuthFailureError {
                 HashMap<String,String> hashMap= new HashMap<String, String>();
                hashMap.put("username",e1.getText().toString());
                 hashMap.put("password",e2.getText().toString());
                 return hashMap;

             }
         };

requestQueue.add(request);
        }
    });

Also my login Url is

http://10.0.2.2/Login.php

The PhP works fine as I have tried and tested it using Postman. Can someone point my mistake over here ? I have looked and looked and can't find any.

Community
  • 1
  • 1
Ayush Aggarwal
  • 215
  • 4
  • 15
  • This happens when volley is not able to connect to the server. This might be happening because of some proxy setting which you have done in your phone. If not then check from you phone web browser whether this link is accessible or not. I suspect it is not. – Arpit Ratan Jun 21 '16 at 19:20
  • Yes , It's not . So is there no way I can use a local host and host my database over there ? – Ayush Aggarwal Jun 21 '16 at 19:27
  • Running the code on emulator will work – Arpit Ratan Jun 21 '16 at 19:39

2 Answers2

0

You cannot access localhost through your phone. It will attempt to connect to 10.0.2.2, but will not find it because it is running on your machine, and not your Android phone.

I would recommend using a service like https://ngrok.com/download to forward your local IP address to a public address you can access from your phone.

user3331142
  • 1,222
  • 1
  • 11
  • 22
0

To achieve what you are trying to do, you have following options :

  1. Run your code in emulator without any changes, it should work.
  2. If you want to run on phone, change the IP address to your PC's IP address. (Change 10.0.2.2 to your PC's IP address)

For more details you can have a look here :

https://futurestud.io/blog/how-to-run-an-android-app-against-a-localhost-api

Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20