我想从远程数据库中获取一些数据,并且我的活动的 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 请求之后”检查。
怎么了?