- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当在同一个类上运行我的代码时,应用程序运行良好。但是当我在两个不同的类上运行此代码时,我的应用程序出现错误 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/
这个问题在这里已经有了答案: How can I fix 'android.os.NetworkOnMainThreadException'? (66 个回答) 关闭2年前。 我刚刚发现 Networ
这个问题在这里已经有了答案: How can I fix 'android.os.NetworkOnMainThreadException'? (65 个回答) 7年前关闭。 嗨,我正在尝试使用 an
这个问题已经有答案了: How can I fix 'android.os.NetworkOnMainThreadException'? (64 个回答) 已关闭 3 年前。 我在 Android 应
我的 Android 项目中有一个名为 EditTrip_Activity.java 的类/Activity ,并且我不断收到此 NetworkOnMainThreadException。我之前已经得
我试图从 MySQL 数据库获取产品的详细信息,但是当我编译代码时,即使我在调用 GetProductDetails 类来执行时将其用作 AsyncTask,也会收到 NetworkOnMainThr
这个问题已经有答案了: How can I fix 'android.os.NetworkOnMainThreadException'? (64 个回答) 已关闭 7 年前。 实际上,我尝试将图像文件
我想将视频(从图库中选择)上传到服务器(localhost-Wampserver 在我的计算机上运行 Apache 2.4.4)。从异常和搜索中,我发现网络操作不应该发生在UI运行的线程中。但我该怎么
我正在尝试将 Android 应用程序与 Twitter 连接,但我不知道这部分代码有什么问题: Thread thread = new Thread(){ @Override
为什么当我在 Android 4.0 版本中运行时,此错误通常出现在 listview 中,任何人都可以帮助我解决这个问题。我搜索了一些网站,发现像在 中使用 asynctask >url 我该如何实
在主线程上没有网络调用,但我仍然得到同样的错误:NetworkOnMainThreadException。 MainActivity.class: public class MainActivity
熟悉Android开发,习惯了在UIThread上进行网络调用时的NetworkOnMainThreadException异常。 但是在下面的情况下会抛出 IOException 而不会抛出 Netw
我了解到在 GUI 线程上不允许进行网络操作。对我来说还可以。但是为什么在 Dialog 按钮点击回调上使用这段代码仍然会产生 NetworkOnMainThreadException ? new T
我正在尝试使用套接字在 android 中编写一个服务器/客户端应用程序,并且我在 AsyncTask 中处理客户端套接字(服务器不是 android,只是普通的 java)。当我得到异常时我正在尝试
我已经通过所有 SO 的解决方案来解决 NetworkOnMainThreadException 包括 ASync 类 - 但仍然有问题 这是我的简单代码: ActivityMain 类: publi
这个问题已经有答案了: How can I fix 'android.os.NetworkOnMainThreadException'? (65 个回答) 已关闭 9 年前。 我开发了一个使用 and
我正在尝试通过启动 TCP 服务器来访问 android 网络。但是当我创建一个新线程时,要么 Thread t = new Thread(runnable); t.start(); 或FutureT
我尝试从 POST 请求中获取响应。问题是,尽管我正在使用 AsyncTask 进行网络通信,但有时我会得到 NetworkOnMainThreadException。这意味着有时我成功获得响应,有时
我按照此说明使用传感器模拟器在模拟器上调试我的应用程序: http://code.google.com/p/openintents/wiki/SensorSimulator#How_to_use_th
我在下面有一些代码: protected void testConnection(String url) { DefaultHttpClient httpclient = new Defaul
我的应用程序中有一些网络请求,每当调用网络相关代码时,我都会收到此异常。我已经把它们放在后台了: @Background protected void getBitmapFromURL
我是一名优秀的程序员,十分优秀!