M
M
max_vr2016-05-18 20:59:07
Java
max_vr, 2016-05-18 20:59:07

Why is my program crashing?

I am writing a method for making get requests. Faced a problem: the program crashes when trying to connect. Experimentally found out that the program crashes on the line connect.connect(); I registered all the necessary permissions in the manifest. What else could be the reason? Thanks in advance

public class connect {

    static public String getData(String _encoding, String _url) {
        try {
            HttpURLConnection connect = (HttpURLConnection) new URL(_url).openConnection();
            connect.setRequestMethod("GET");
            connect.setRequestProperty("Content-length", "0");
            connect.setUseCaches(false);
            connect.setAllowUserInteraction(false);
            connect.setConnectTimeout(2000);
            connect.setReadTimeout(2000);
            connect.connect(); // НЕ РАБОТАЕТ :((

            int status = connect.getResponseCode();

            switch (status) {
                case 200: return "Error1";
                case 201:
                    BufferedReader br = new BufferedReader(new InputStreamReader(
                            connect.getInputStream(), _encoding));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    br.close();
                    return sb.toString();
            }
        } catch (MalformedURLException ex) {
            return "Error2";
        } catch (IOException ex) {
            return "Error3";
        }
        return "Error4";
    }
}

Logs:
05-18 21:02:15.708 2145-2145/com.test.vkimport.vkimport E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.vkimport.vkimport, PID: 2145
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.vkimport.vkimport/com.test.vkimport.vkimport.test}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
at android.app.ActivityThread.handleLaunchActivity( ActivityThread.java:2420)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android .os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
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: 904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: 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 com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal .http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106
) .connect.getData(connect.java:24)
at com.test.vkimport.vkimport.test.onCreate(test.java:14)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
at android.app. ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5294) 
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:904) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
05-18 21:02:17.860 2145- 2145/com.test.vkimport.vkimport I/Process: Sending signal. PID: 2145 SIG: 9

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lazard105, 2016-05-19
@lazard105

It is written after all: NetworkOnMainThreadException.
The request must be executed in a separate thread.
Call your method like this:

new Thread(){
            @Override
            public void run() {
                 connect.getData(_encoding, _url)
            }
        }.start();

G
GavriKos, 2016-05-18
@GavriKos

Where are the logs?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question