gpt4 book ai didi

android - 错误 android.os.networkonmainthreadexception asynctask getResponseCode

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:48 25 4
gpt4 key购买 nike

当在同一个类上运行我的代码时,应用程序运行良好。但是当我在两个不同的类上运行此代码时,我的应用程序出现错误 android.os.networkonmainthreadexception。调试时,我在 responseHttp = httpConnection.getResponseCode(); 检测到错误应用程序运行到 responseHttp = httpConnection.getResponseCode(); 行,它会捕获{},取消“If..else”。并记录错误 android.os.networkonmainthreadexception。你能帮帮我吗!!

我的代码类Asynctask

package com.example.finishdemo;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.os.AsyncTask;

public class TestConnectionNew extends AsyncTask<String, Void, String> {
private int responseHttp = 0;
private String flag="false";

@Override
protected String doInBackground(String... urltest) {
// TODO Auto-generated method stub
try {
URL url = new URL(urltest[0]);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(2000);
HttpURLConnection httpConnection = (HttpURLConnection) connection;
**responseHttp = httpConnection.getResponseCode();**
if (responseHttp == HttpURLConnection.HTTP_OK) {
flag = "true";
} else {
flag = "false";
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Thong bao loi: "+e.toString());
}
return flag;
}
}

主要代码类:

package com.example.finishdemo;
public class Hoadon extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hoadon);
TestConnectionNew t=new TestConnectionNew();
String recieve=t.doInBackground("http://longvansolution.tk/monthlytarget.php");
if(recieve.equalsIgnoreCase("true"))
{
doTimerTask();
}else
if(recieve.equalsIgnoreCase("false"))
{
showAlert("Không kết nối được đến server hoặc thiết bị chưa có kết nối internet!");
}

最佳答案

使用asynctask.execute用于执行 AsyncTask 而不是手动调用 doInBackground:

 TestConnectionNew t = new TestConnectionNew();
t.execute("http://longvansolution.tk/monthlytarget.php");

将您的 TestConnectionNew 更改为

public class TestConnectionNew extends AsyncTask<String, Void, String> {
private int responseHttp = 0;
private String flag="false";

@Override
protected String doInBackground(String... urltest) {
// TODO Auto-generated method stub
try {
URL url = new URL(urltest[0]);
URLConnection connection = url.openConnection();
connection.setConnectTimeout(2000);
HttpURLConnection httpConnection = (HttpURLConnection) connection;
**responseHttp = httpConnection.getResponseCode();**
if (responseHttp == HttpURLConnection.HTTP_OK) {
flag = "true";
} else {
flag = "false";
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Thong bao loi: "+e.toString());
}
return flag;
}

@Override
protected void onPostExecute(String recieve) {
if(recieve.equalsIgnoreCase("true"))
{
doTimerTask();
}else
if(recieve.equalsIgnoreCase("false"))
{
showAlert("Không kết nối được đến server hoặc thiết bị chưa có kết nối internet!");
}
}
}

有关我们如何使用 AsyncTask 的更多帮助,请参阅:

http://developer.android.com/reference/android/os/AsyncTask.html

关于android - 错误 android.os.networkonmainthreadexception asynctask getResponseCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13739773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com