Страницы

Поиск по вопросам

вторник, 16 июля 2019 г.

Ошибка при загрузке PHP скрипта из интернета. Java Android [дубликат]

На данный вопрос уже ответили: Как починить android.os.NetworkOnMainThreadException 1 ответ Ругается на функцию postValues, на строку:
OutputStream os = conn.getOutputStream();
try { String result = LoginActivity.postValues("http://stopdps.ru/app/login.php", "login="+userlogin+"&hash="+userhash); Log.e("bertam", result); } catch (Exception e) { e.printStackTrace(); } }
public static String postValues (String strUrl, String strJsonArray) throws Exception{ URL url = new URL(strUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Log.e("result",""+strJsonArray);
OutputStream os = conn.getOutputStream(); os.write(strJsonArray.getBytes()); os.flush();
conn.connect();
if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); }
BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream())));
String output; StringBuilder sb = new StringBuilder();
System.out.println("Output from Server ....
"); while ((output = br.readLine()) != null) { Log.e("output",output); sb.append(output);
}
Log.e("output","result: "+sb.toString());
conn.disconnect();
return sb.toString(); }
Вот ошибка:
01-13 03:54:50.026 W/System.err( 3120): android.os.NetworkOnMainThreadException 01-13 03:54:50.026 W/System.err( 3120): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) 01-13 03:54:50.026 W/System.err( 3120): at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 01-13 03:54:50.026 W/System.err( 3120): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 01-13 03:54:50.026 W/System.err( 3120): at java.net.InetAddress.getAllByName(InetAddress.java:214) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89) 01-13 03:54:50.026 W/System.err( 3120): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197) 01-13 03:54:50.026 W/System.err( 3120): at bertam.stopdps.LoginActivity.postValues(LoginActivity.java:262) 01-13 03:54:50.026 W/System.err( 3120): at bertam.stopdps.MapActivity$2.onMyLocationChange(MapActivity.java:113) 01-13 03:54:50.026 W/System.err( 3120): at com.google.android.gms.maps.GoogleMap$4.zzq(Unknown Source) 01-13 03:54:50.026 W/System.err( 3120): at com.google.android.gms.maps.internal.zzs$zza.onTransact(Unknown Source) 01-13 03:54:50.026 W/System.err( 3120): at android.os.Binder.transact(Binder.java:361) 01-13 03:54:50.026 W/System.err( 3120): at com.google.android.gms.maps.internal.bq.a(SourceFile:82) 01-13 03:54:50.026 W/System.err( 3120): at com.google.maps.api.android.lib6.e.al.a(Unknown Source) 01-13 03:54:50.026 W/System.err( 3120): at com.google.maps.api.android.lib6.e.al.a(Unknown Source) 01-13 03:54:50.026 W/System.err( 3120): at com.google.maps.api.android.lib6.e.x.run(Unknown Source) 01-13 03:54:50.026 W/System.err( 3120): at android.os.Handler.handleCallback(Handler.java:733) 01-13 03:54:50.026 W/System.err( 3120): at android.os.Handler.dispatchMessage(Handler.java:95) 01-13 03:54:50.026 W/System.err( 3120): at android.os.Looper.loop(Looper.java:136) 01-13 03:54:50.026 W/System.err( 3120): at android.app.ActivityThread.main(ActivityThread.java:5001) 01-13 03:54:50.026 W/System.err( 3120): at java.lang.reflect.Method.invokeNative(Native Method) 01-13 03:54:50.026 W/System.err( 3120): at java.lang.reflect.Method.invoke(Method.java:515) 01-13 03:54:50.026 W/System.err( 3120): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 01-13 03:54:50.026 W/System.err( 3120): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 01-13 03:54:50.026 W/System.err( 3120): at dalvik.system.NativeStart.main(Native Method)


Ответ

android.os.NetworkOnMainThreadException -- вы в UI-потоке пытаетесь с сетью работать. Вынесите вызов postValues в отдельный поток.

Комментариев нет:

Отправить комментарий