关于LocalHost错误的Android Volley库

时间:2017-01-19 14:29:48

标签: android web-services

当在localhost上运行时,Volley库获取错误(无法连接到服务器)我的web服务在asp.net中。在浏览器上工作得很好,但在android设备中我不对,请帮助我。我是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);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

在本地主机的位置使用http://127.0.0.1:5000/并查看