Добавил
<uses-permission android:name="android.permission.INTERNET" />
в андроидМанифест. Функция, которую я использую:
public String getData(int _timeout, 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(_timeout);
connect.setReadTimeout(_timeout);
connect.connect();
int status = connect.getResponseCode();
switch (status) {
case 304:
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) {
//код обработки ошибки
} catch (IOException ex) {
//код обработки ошибки
}
return "NOTHING";
}
Вызов функции:
String test = getData(10000, "UTF-8", "http://google.com/");
Ошибка, которая появляется в logcat
05-09 18:48:35.003 22421-22421/com.test_app E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:3609) at android.view.View.performClick(View.java:4102) at android.view.View$PerformClick.run(View.java:17085) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:155) at android.app.ActivityThread.main(ActivityThread.java:5511) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at android.view.View$1.onClick(View.java:3604) at android.view.View.performClick(View.java:4102) at android.view.View$PerformClick.run(View.java:17085) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:155) at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method) Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1208) at java.net.InetAddress.lookupHostByName(InetAddress.java:388) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:239) at java.net.InetAddress.getAllByName(InetAddress.java:214) at libcore.net.http.HttpConnection.(HttpConnection.java:70) at libcore.net.http.HttpConnection.(HttpConnection.java:50) at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) at libcore.net.http.HttpEngine.connect(HttpEngine.java:310) at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) at com.test_app.MainActivity.getData(MainActivity.java:45) at com.test_app.MainActivity.onClick(MainActivity.java:211)
at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at android.view.View$1.onClick(View.java:3604) at android.view.View.performClick(View.java:4102) at android.view.View$PerformClick.run(View.java:17085) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:155) at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)