我正在尝试使用WiFi来检索数据,但应用程序正在崩溃。当我使用localHost仿真器时,它工作得很好但是当我使用移动数据或WiFi时它会崩溃。我可以使用WiFi将数据保存到localhost上的MySql,但我无法接收数据。有些时候我会android.os.NetworkonMainThreadException
。我是一个非常新的程序员,只是借用了大部分代码,需要明确说明如何解决这个问题。
/**
* Background Async Task to Get complete User details
* */
class GetUserDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting User details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
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("user_ID", "2"));
// getting User details by making HTTP request
// Note that User details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "GET", params);
// check your log for json response
Log.d("Single User Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received User details
JSONArray UserObj = json
.getJSONArray(TAG_User); // JSON Array
// get first User object from JSON Array
JSONObject User = UserObj.getJSONObject(0);
// User with this pid found
// Edit Text
txtlname = (EditText) findViewById(R.id.editText1);
txtfname = (EditText) findViewById(R.id.editText2);
// display User data in EditText
txtfname.setText(User.getString(TAG_FNAME));
txtlname.setText(User.getString(TAG_LNAME));
}else{
// User with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
答案 0 :(得分:2)
实际上你不应该在UI线程中连接到互联网。
您通过JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "GET", params);
中的runOnUiThread( new Runnable....
与互联网建立了连接。
/**
* Background Async Task to Get complete User details
* */
class GetUserDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting User details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_ID", "2"));
// getting User details by making HTTP request
// Note that User details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "GET", params);
// check your log for json response
Log.d("Single User Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received User details
JSONArray UserObj = json
.getJSONArray(TAG_User); // JSON Array
// get first User object from JSON Array
final JSONObject User = UserObj.getJSONObject(0);
runOnUiThread(new Runnable() {
public void run() {
// User with this pid found
// Edit Text
txtlname = (EditText) findViewById(R.id.editText1);
txtfname = (EditText) findViewById(R.id.editText2);
// display User data in EditText
txtfname.setText(User.getString(TAG_FNAME));
txtlname.setText(User.getString(TAG_LNAME));
}
});
}else{
// User with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
正如您所看到的,当我想处理视图(如文本视图)时,我刚刚返回UI线程。注意user
变量。我最后决定让它在我用作Runnable
方法参数的runOnUiThread
对象中可以访问。
答案 1 :(得分:0)
您不应该在doInBackground中执行与UI相关的代码,您应该将UI相关代码移动到AsyncTask的onPostExecute或onProgressUpdate方法。
这个答案可能对您有所帮助:
Android : Calling the methods on UI thread from AsyncTask doInBackground method