我试图使用WiFi回顾数据,但应用程序崩溃。当我使用localHost模拟器时,它工作得很好,但是当我使用移动数据或WiFi时,它会崩溃。我可以使用WiFi将数据保存到本地主机上的MySql,但我不能接收数据。有些时候我得到android.os.NetworkonMainThreadException
。我是一个非常新的程序员,只是借用了大部分代码,并且需要清楚地说明如何解决这个问题。使用JSON和MySql使用Android应用程序回收数据
/**
* 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();
}
}