我想从远程数据库中获取一些数据,并且在我的活动的AsyncTask子类中有一些网络,但是当我尝试加载我的程序时,它显示了NetworkOnMainThreadException。
这是我的代码:
public class MyMapLocationActivity extends MapActivity {
private MapView mapView;
private static String url_product_details = "http://myurl.com/readfromdb";
private static final String TAG_SUCCESS = "success";
private static final String TAG_LATITUDE = "latitude";
private static final String TAG_LONGITUDE = "longitude";
private static final String TAG_POINT = "point";
JSONParser jsonParser = new JSONParser();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
ReadLocations Read = new ReadLocations();
Read.execute();
}
//And my networking AsyncTask:
class ReadLocations extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
Log.d("debug", "START DOINBACKGROUND");
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "16"));
// getting product details by making HTTP request
// Note that product details url will use GET request
Log.d("debug", "before http request");
JSONObject json = jsonParser.makeHttpRequest(
url_product_details, "POST", params);
Log.d("debug", "after http request");
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_POINT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
@Override
protected void onPostExecute(String text) {
// dismiss the dialog once done
Log.d("debug", "ONPOSTEXECUTE");
}
LogCat显示“在http请求之前”,但没有显示我的“在http请求之后”检查。
有什么问题?
答案 0 :(得分:5)
您的网络代码位于应用程序的 MainUiThread 不在工作线程中...
删除
runOnUiThread(new Runnable() {
public void run() {
来自doInBackground()
并将 WebRequest 代码仅放在AsyncTask的doInBackground()
中。
<强>更新强>
实际上doInBackground()
的 AsyncTask 实现是错误的。由于doInBackground()
在工作线程中运行以进行冗长且与网络相关的操作。并且您正尝试使用runOnUiThread()
来运行 MainUiThread ,而不是规则。如果您想要进行任何与UI相关的工作,请在onPostExecute()
onPreExecute()
中进行和其他ASyncTask方法。
答案 1 :(得分:1)
快来看看这段代码:
@Override
protected String doInBackground(String... args) {
Log.d("debug", "START DOINBACKGROUND");
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "16"));
// getting product details by making HTTP request
// Note that product details url will use GET request
Log.d("debug", "before http request");
JSONObject json = jsonParser.makeHttpRequest(
url_product_details, "POST", params);
Log.d("debug", "after http request");
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_POINT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
您的代码的问题在于您只在主UI线程上运行网络功能,使用:runOnUiThread(new Runnable() {...}
。
doInBackground()自动在单独的后台非ui线程上运行,您无需显式创建线程。
答案 2 :(得分:0)
那是因为您在UI线程上运行网络代码...您的doInBackground
在UI线程上显式运行网络代码。