微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。