@KirillXD

Причина вылета приложения из Android Studio?

При выполнении get запроса приложение вылетает

EditText log = (EditText) findViewById(R.id.editText6);
String login = String.valueOf(log.getText());
EditText pas = (EditText) findViewById(R.id.editText7);
String pass = String.valueOf(pas.getText());
login = login.replace(" ","-");
pass = pass.replace(" ","-");

URL obj = new URL("тут ссылка");
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();

//add reuqest header
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0" );
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
connection.setRequestProperty("Content-Type", "application/json");

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = bufferedReader.readLine()) != null) {
response.append(inputLine);
}
bufferedReader.close();

Intent intent = new Intent(this, MENU.class);
EditText editText = (EditText) findViewById(R.id.editText6);
String message = editText.getText().toString();

startActivity(intent);

LogCat:

03-07 20:29:29.184 24090-24090/ru.my1.finebot.ogsecurity E/AndroidRuntime: FATAL EXCEPTION: main
Process: ru.my1.finebot.ogsecurity, PID: 24090
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5057)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11159)
at android.view.View$PerformClick.run(View.java:23752)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5052)
at android.view.View.performClick(View.java:6261) 
at android.widget.TextView.performClick(TextView.java:11159) 
at android.view.View$PerformClick.run(View.java:23752) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:209)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:163)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:105)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:489)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:503)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:438)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:247)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
at ru.my1.finebot.ogsecurity.MainActivity.onButtonClick(MainActivity.java:52)
at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:5052) 
at android.view.View.performClick(View.java:6261) 
at android.widget.TextView.performClick(TextView.java:11159) 
at android.view.View$PerformClick.run(View.java:23752) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 

P.S.
Выделил те строчки, на которых ошибка
  • Вопрос задан
  • 515 просмотров
Решения вопроса 1
zagayevskiy
@zagayevskiy Куратор тега Java
Android developer at Yandex
Он же тебе чёрным по белому пишет, что NetworkOnMainThreadException. В сеть нельзя ходить на ui-потоке.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы