K
K
KirillXD2018-03-07 20:34:26
Java
KirillXD, 2018-03-07 20:34:26

Reason for app crash from Android Studio?

When a get request is made, the application crashes
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("link here");
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().
startActivity(intent);
LogCat:
03-07 20:29:29.184 24090-24090/en.my1.finebot.ogsecurity E/AndroidRuntime: FATAL EXCEPTION: main
Process: en.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 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
)
.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) 
PS
Highlighted those lines where the error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-03-07
@KirillXD

He writes to you in black and white that NetworkOnMainThreadException. You cannot go to the network on the ui-stream.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question