我是Android开发的新手,并尝试创建一个简单的应用程序,它应该联系Web服务器并获取请求Api。
我在InteliJ IDEA上编写这个简单的java代码并正常工作
但是在Android Studio中写类似的东西时会得到一个android.os.NetworkOnMainThreadException
任何人都可以解释为什么会出现异常
InteliJ IDEA代码:
package sample;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://codeforces.com/api/user.rating?handle=Adel_SaadEddin");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
StringBuilder stringBuilder = new StringBuilder();
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
stringBuilder.append(current);
}
String res = stringBuilder.toString();
System.out.print(res);
} catch (Exception ex) {
System.out.print("Error");
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
}
Android Studio:
MainActivity.java
package android.adelsaadeddin.app3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.text);
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://codeforces.com/api/user.rating?handle=Adel_SaadEddin");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
StringBuilder stringBuilder = new StringBuilder();
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
stringBuilder.append(current);
}
String res = stringBuilder.toString();
textView.setText(res);
} catch (Exception ex) {
textView.setText("Error");
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.adelsaadeddin.app3">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="android.adelsaadeddin.app3.MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Logcat:
01-19 15:03:32.812 4379-4379/? I/art: Not late-enabling -Xcheck:jni (already on)
01-19 15:03:32.813 4379-4379/? W/art: Unexpected CPU variant for X86 using defaults: x86
01-19 15:03:32.885 4379-4379/android.adelsaadeddin.app3 W/ActivityThread: Application android.adelsaadeddin.app3 is waiting for the debugger on port 8100...
01-19 15:03:32.887 4379-4379/android.adelsaadeddin.app3 I/System.out: Sending WAIT chunk
01-19 15:03:34.385 4379-4386/android.adelsaadeddin.app3 I/art: Debugger is active
01-19 15:03:34.513 4379-4379/android.adelsaadeddin.app3 I/System.out: Debugger has connected
01-19 15:03:34.514 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:34.715 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:34.917 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.119 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.321 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.527 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.731 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:35.932 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:36.133 4379-4379/android.adelsaadeddin.app3 I/System.out: waiting for debugger to settle...
01-19 15:03:36.335 4379-4379/android.adelsaadeddin.app3 I/System.out: debugger has settled (1471)
01-19 15:03:36.444 4379-4379/android.adelsaadeddin.app3 W/System: ClassLoader referenced unknown path: /data/app/android.adelsaadeddin.app3-1/lib/x86
01-19 15:03:36.561 4379-4379/android.adelsaadeddin.app3 I/InstantRun: starting instant run server: is main process
01-19 15:03:36.705 4379-4386/android.adelsaadeddin.app3 I/art: Starting a blocking GC Instrumentation
01-19 15:03:36.850 4379-4379/android.adelsaadeddin.app3 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-19 15:03:47.223 4379-4379/android.adelsaadeddin.app3 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-19 15:05:40.325 4379-4449/android.adelsaadeddin.app3 I/OpenGLRenderer: Initialized EGL, version 1.4
01-19 15:05:40.326 4379-4449/android.adelsaadeddin.app3 D/OpenGLRenderer: Swap behavior 1
01-19 15:05:40.326 4379-4449/android.adelsaadeddin.app3 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
01-19 15:05:40.326 4379-4449/android.adelsaadeddin.app3 D/OpenGLRenderer: Swap behavior 0
01-19 15:05:40.389 4379-4449/android.adelsaadeddin.app3 D/EGL_emulation: eglCreateContext: 0xa658e9e0: maj 2 min 0 rcv 2
01-19 15:05:40.717 4379-4379/android.adelsaadeddin.app3 W/art: Verification of void android.support.v7.widget.AppCompatImageView.setBackgroundResource(int) took 135.260ms
01-19 15:05:40.735 4379-4379/android.adelsaadeddin.app3 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
01-19 15:05:40.811 4379-4449/android.adelsaadeddin.app3 D/EGL_emulation: eglMakeCurrent: 0xa658e9e0: ver 2 0 (tinfo 0xa3bcd120)
01-19 15:05:40.868 4379-4449/android.adelsaadeddin.app3 D/EGL_emulation: eglMakeCurrent: 0xa658e9e0: ver 2 0 (tinfo 0xa3bcd120)
答案 0 :(得分:0)
您无法在应用程序的主线程中发送请求。您应该使用AsyncTask或后台线程来发送请求。 (我建议你使用Volley或Retrofit)检查这些链接:
How do I fix android.os.NetworkOnMainThreadException?
http://simpledeveloper.com/network-on-main-thread-error-solution/
答案 1 :(得分:0)
您正在主线程中进行长时间操作,抛出异常
new AsyncTask<String,String,String>(){
@Override
protected String doInBackground(String... params) {
//Your HTTP Request goes here
return response;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("UpdateJSON ", s);
if(!s.equalsIgnoreCase("null") || !s.equalsIgnoreCase(""))
writeManifestJSON(s);
}
}.execute();
我使用新的Thread
解决了这个问题。哪个在主线程中不起作用。
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your HTTP Request goes here
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();