android volley - 无法从本地主机读取数据

问题描述

我编写了 android 应用程序来使用 volley 读取 json。

如果我使用来自互联网的示例 URL - 我没有问题,数据被读取。但是当我尝试从本地主机读取数据时,出现错误。 我可以在模拟器的网络浏览器中看到相同的数据。 我的网址:“http://10.0.2.2/volley_sample/get_data.PHP

我的代码

Pycharm DB browser Console

adres "http://10.0.2.2/volley_sample/get_data.PHP" 在我的模拟器的网络浏览器中工作。 我也试过“http://192.168....”、“127.0.0.1....”、“localhost/volley_sample....”等,但都不起作用。

adres "https://reqres.in/api/users/2"(来自互联网的例子)工作没有问题。 ofc.我添加 private void sendGetRequest() { RequestQueue queue = Volley.newRequestQueue(MainActivity.this); String url = "http://10.0.2.2/volley_sample/get_data.PHP"; //it doesn't work //String url ="https://reqres.in/api/users/2"; //it works // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET,url,new Response.Listener<String>() { @Override public void onResponse(String response) { // display the first 10 characters of the response string. get_response_text.setText("Response is: " + response.substring(0,10)); } },new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { get_response_text.setText("That didn't work!"); } }); // Add the request to the RequestQueue. queue.add(stringRequest); }

我不知道我做错了什么:(

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)