K
K
koritesuk2021-01-13 16:33:55
Java
koritesuk, 2021-01-13 16:33:55

Can't send http request to server, what's wrong?

I want to send latitude and longitude data to my server via http, but it gives an error in the manifest permission to use the Internet requested
Error - E / AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.drivein, PID: 5716
android.os.NetworkOnMainThreadException
at android .os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net. InetAddress.getAllByName(InetAddress.java:215)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:185)
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:172)
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:130)
org.apache.http.impl .client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1317)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:707)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient .java:696)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)
at com.example.drivein.MainActivity.formatLocation(MainActivity.java:130)
at com.example.drivein.MainActivity.showLocation(MainActivity.java:113)
at com.example.drivein.MainActivity.access$000(MainActivity.java: 34)
at com.example.drivein.MainActivity$1.onLocationChanged(MainActivity.java:82)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager .java:210)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect .Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200 )
I/Process: Sending signal. PID: 5716 SIG: 9
code

private String formatLocation(Location location) {
        if (location == null)
            return "";

        String a, b, answerHTTP;
        a = ((Double) location.getLatitude()).toString();
        b = ((Double) location.getLatitude()).toString();
        String server = "http://demo.harrix.org/demo0011";
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(server + "?a=" + a + "&b=" + b);
        try {
            HttpResponse response = httpclient.execute(httpget);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                answerHTTP = EntityUtils.toString(entity);
            }
        }
        catch (ClientProtocolException e) {

        }
        catch (IOException e) {
        }

        return String.format(
                "Coordinates: lat = %1$.4f, lon = %2$.4f, time = %3$tF %3$tT",
                location.getLatitude(), location.getLongitude(), new Date(
                        location.getTime()));}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2021-01-13
@iLLuzor

1) We look at the error stack trace;
2) We see in the first line android.os.NetworkOnMainThreadException ;
3) We look at what is written about this exception in the documentation ;
4) We think and solve the problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question