错误android.os.networkonmainthreadexception异步任务getResponseCode
当在同一类上运行我的代码时,应用程序运行良好。但当我在两个不同的类上运行此代码时,我的应用程序出现错误android.os.networkonmainthreadexception。 调试时,我在responseHttp=httpConnection.getResponseCode()处检测到错误强> 应用程序运行到lineresponseHttp=httpConnection.getResponseCode()并转到捕捉{},取消“If..else”,并记录错误android.os.networkonmainthreadexception。 你能帮我吗 我的代码类异步任务错误android.os.networkonmainthreadexception异步任务getResponseCode,android,Android,当在同一类上运行我的代码时,应用程序运行良好。但当我在两个不同的类上运行此代码时,我的应用程序出现错误android.os.networkonmainthreadexception。 调试时,我在responseHttp=httpConnection.getResponseCode()处检测到错误 应用程序运行到lineresponseHttp=httpConnection.getResponseCode()并转到捕捉{},取消“If..else”,并记录错误android.os.network
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;
}
}
用于执行AsyncTask,而不是手动调用doInBackground,如下所示:
TestConnectionNew t = new TestConnectionNew();
t.execute("http://longvansolution.tk/monthlytarget.php");
将测试连接更改为新的
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!");
}
}
}
公共类TestConnectionNew扩展异步任务{
私有int-responseHttp=0;
私有字符串标志=“false”;
@凌驾
受保护的字符串doInBackground(字符串…urltest){
//TODO自动生成的方法存根
试一试{
URL=新URL(urltest[0]);
URLConnection=url.openConnection();
connection.setConnectTimeout(2000);
HttpURLConnection httpConnection=(HttpURLConnection)连接;
**responseHttp=httpConnection.getResponseCode()**
if(responseHttp==HttpURLConnection.HTTP\u确定){
flag=“true”;
}否则{
flag=“false”;
}
}捕获(例外e){
e、 printStackTrace();
System.out.println(“Thong-bao-loi:+e.toString());
}
返回标志;
}
@凌驾
PostExecute上受保护的void(字符串接收){
if(接收相等信号情况(“真”))
{
doTimerTask();
}否则
if(接收同等信号情况(“假”))
{
showAlert(“Không kết nốiđượcđến服务器hoặc thiếTBị 契亚·科克ết nố我喜欢互联网!);
}
}
}
有关如何使用AsyncTask的更多帮助,请参阅:
在AndroidManifest.xml中
您可以添加一个代码:
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
我认为这不是调用AsyncTask的方式;“并收到此通知”类型不匹配:无法从AsyncTask转换为字符串“@user1817079:请参阅我的编辑答案以更新UI,您需要覆盖OnPostExecute,如果您使用字符串recieve=t.execute(“longvansolution.tk/monthlytarget.php”)。get();然后这也将挂起UI线程,直到控件没有从AsyncTask返回到主线程谢谢…我使用字符串recieve=t.execute(“longvansolution.tk/monthlytarget.php”).get();并运行success…@user1817079:如果您想在后台发出请求而不等待,请参见这里使用execute.get();
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"