我正在开发一个用于预订的Android应用程序。这是我关于stackoverflow的第一个问题。所以如果我没有严格遵守规则请原谅我。我遇到了这个致命的错误:
FATAL EXCEPTION: main
Process: com.connectandroiddb.myapplication, PID: 26440
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:199)
at com.example.myapplication.JSONParser.makeHttpRequest(JSONParser.java:65)
at com.example.myapplication.MainActivity$GetProductDetails$1.run(MainActivity.java:144)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)我借用了下面的代码:https://androidhive.info/2012/05/how-to-connect-android-with-php-mysql/相关代码:我调用GetProductDetails(),如下所示
public void sendMessage(View view) {
.....
new GetProductDetails().execute();
......
}它是MainActivity.java格式的
/**
* Background Async Task to Get complete product details
* */
class GetProductDetails extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
JSONObject json = jsonParser.makeHttpRequest(
url_get_product_details, "GET");// Line 144
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
}
} 以下是来自JSONParser的相关部分:
private String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
public JSONObject makeHttpRequest(String url, String method)
{
HttpURLConnection urlConnection = null;
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST CHECK
}else if(method == "GET"){
// request method is GET
URL u = new URL("http://example.com/");
urlConnection = (HttpURLConnection) u.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); //JSONParser.java:65
json=readStream(in);
}
}
catch (MalformedURLException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
finally {
if (urlConnection != null) {
try {
urlConnection.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}我已经在AndroidManifest.xml中添加了互联网权限。请帮帮忙!。提前谢谢你,
发布于 2019-03-16 19:02:48
您的问题出在代码的这一部分:
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
JSONObject json = jsonParser.makeHttpRequest(url_get_product_details, "GET");
}
});
return null;
}正如您所看到的,在方法doInBackground中,您正在UI线程上执行一些操作。Android在同一个UI线程中运行所有UI,您不能在该线程上执行任何长时间运行的任务(如网络请求),因为UI将保持冻结状态。
这就是人们使用异步任务或简单线程的原因。如果在doInBackground方法中执行runOnUiThread,您就违反了该规则,并且NetworkOnMainThreadException将引发异常。
阅读教程here,了解如何使用AsyncTask在后台和UI之间传递信息。提示:您需要使用onPostExecute
https://stackoverflow.com/questions/55195953
复制相似问题