Пишу метод для осуществления get-запросов. Столкнулся с проблемой: программа вылетает, при попытке подключения. Экспериментальным путём выяснил, что программа вылетает именно на строчке connect.connect(); Все нужные разрешения в манифесте прописал. В чем еще может быть причина? Заранее спасибо
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";
}
}
Логи:
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)
at com.test.vkimport.vkimport.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