简体   繁体   中英

In android studio StringRequest can connect to internet but not to local host with ip address?

I have designed android app that send URL "StringRequest.GET" as IP address to local host which is ESP32 Webserver (a microcontroller ) and this returns a web page response, was working fine with my old phone having version EMUI 8.

But now in my new phone with EMUI 10 there is no response from the ESP32 web server I changed the URL to ("www.google.com") it work fine, but when putting local IP ("192.168.0.116") which is the ESP address it does not work.

Strangely in the same new phone when I use phone web browser the connection is fine and get good response from the IP address

I wonder what is the problem Can any one help me, may be some new feature added to newer versions of EMUI, but I took long time changing lot of things IP addresses, ports, start new app from scratch change ESP programming but all fail.

This is my code:

try {
            
            StrictMode.ThreadPolicy policy = new 
         StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            
            RequestQueue queue = Volley.newRequestQueue(this);
            String url ="https://192.168.0.116";
            //String url ="https://www.google.com";

            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! "+ String.valueOf(x));
                }
            });

     // Add the request to the RequestQueue.
            queue.add(stringRequest);
        }catch (Exception e){
            Toast.makeText(this, e.getMessage().toString(), Toast.LENGTH_LONG).show();

        }

I don't know exactly how, I made few changes and it work fine, these are:

First:

changed the (network_security_config.xml) to:

'''
 <?xml version="1.0" encoding="utf-8"?>
 <network-security-config>
  <base-config cleartextTrafficPermitted="true">
    <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
  </base-config>
 </network-security-config>
'''

it was like this:

'''
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
    <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
</domain-config>
</network-security-config>
'''

second:

I changed the URL to:

'''
String urlx ="http://192.168.4.1:443/smm";
'''

I don't know why then URL request insist on port 433?! I change it in the vb.net and also in the ESP32 controller

it work finally fine thank you all

You only have to set to items:

  1. include android:usesCleartextTraffic="true"; in the manifast file if using http and
  2. set your host IP to 10.0.2.2/24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM