关于LocalHost错误的Android Volley库

问题描述 投票:1回答:1

在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);
        }
    });
}
android web-services
1个回答
0
投票

在当地主人的地方使用http://127.0.0.1:5000/并查看

© www.soinside.com 2019 - 2024. All rights reserved.