2

Volley library getting error (can't connect to server) when running on localhost my web-service is in asp.net. working perfectly on browser but not in android device where I am wrong please help me. I am new in android

public static final String JSON_URL="http://localhost:1462/Service1.svc/GetCities";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    btn = (Button) findViewById(R.id.btn);
    linearLayoutManager = new LinearLayoutManager(this);
    final MyAdapter ma = new MyAdapter(this, list);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(ma);

    requestQueue = Volley.newRequestQueue(this);
    /////////////////////////////////////////////////////////////
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Toast.makeText(MainActivity.this, "Response", Toast.LENGTH_SHORT).show();

                    try {
                        if (response.length() > 0) {
                            Toast.makeText(MainActivity.this, "Geting Response", Toast.LENGTH_SHORT).show();
                            list.clear();
                            for (int i = 0; i < response.length(); i++) {
                                JSONObject jsonObject = response.getJSONObject(i);
                                Hotels h = new Hotels();
                                if (!jsonObject.isNull("City")) {
                                    h.CityName = jsonObject.getString("City");
                                }

                                list.add(i, h);
                            }
                            ma.notifyDataSetChanged();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // do something
                    Toast.makeText(MainActivity.this, "Cant connect to server", Toast.LENGTH_SHORT).show();
                }
            });

            requestQueue.add(jsonArrayRequest);
        }
    });
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Innocent
  • 703
  • 10
  • 21
  • 1
    Your url is `localhost` for it call your server from device you would need the ip address of your server or a domain – Suraj Rao Jan 23 '17 at 05:27
  • 1
    you can not use `localhost` for emulator its diff than your computer, Instead of `localhost` you can use `10.0.2.2` this will help you http://stackoverflow.com/questions/21510311/10-0-2-28080-in-web-browser-in-emulator-is-not-working for device use your machine's IP – Gaurav Jan 23 '17 at 05:27

1 Answers1

1

use http://127.0.0.1:5000/ at the place of local host and see

V_for_Vj
  • 849
  • 1
  • 12
  • 30