无法向本地服务器发出请求

时间:2020-07-07 16:33:00

标签: java android-studio android-volley

因此,我将从android studio开始,并遵循所有教程。

我对向本地服务器发出请求很感兴趣,所以我用谷歌搜索并看到了它。

https://developer.android.com/training/volley/simple

我按照说明进行操作,被卡在java和android studio的新用户的几个位置上,但最终一切正常。

然后,知道该应用程序将运行,只需使用服务器的IP更改示例中的“ http://www.google.com”字符串即可。没有任何反应。

它不检索索引页面。 我尝试使用PHP脚本。没有。 电话和服务器位于同一局域网中。 我尝试将“ http://”添加到ip,什么也没有。 服务器只能在任何浏览器,PC和手机上正常工作。

我想念什么吗?

如果需要代码,我将其上载,但是在示例中完全相同。

public class MainActivity extends     AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void sendRequest(){
        final TextView textView = (TextView) findViewById(R.id.stringRequest);
        RequestQueue queue = Volley.newRequestQueue(this);
        String url ="http://192.168.1.36";
        // 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 500 characters of the response string.
                textView.setText("Response is: "+ response.substring(0,500));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                textView.setText("That didn't work!");
            }
        });

// Add the request to the RequestQueue.
        queue.add(stringRequest);
    }
    public void onClick(View view){
        sendRequest();
    }
}

Logcat引发此错误:

D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false

此外,我尝试在模拟器上运行它,结果是相同的。

我也添加了与网络相关的所有权限。

1 个答案:

答案 0 :(得分:0)

如果您唯一更改的是将https://www.google.com更改为http://1.2.3.4(请注意,将https切换为http,并将协议保留在字符串URL中),并且该方法在google上可以正常使用,但不适用于您的IP ,那么这意味着无论您在哪里运行此代码(直接在电话上或在开发环境中的仿真器上)都无法到达该IP。请注意,如果您稍等片刻,该代码将出错,例如“目标无法到达”错误或其他启发性提示。