gpt4 book ai didi

java - 使用 JSON 和 MySql 使用 Android 应用程序检索数据

转载 作者:行者123 更新时间:2023-11-29 07:29:29 26 4
gpt4 key购买 nike

我正在尝试使用 WiFi 检索数据,但应用程序崩溃了。当我使用 localHost 模拟器时,它工作得很好,但当我使用移动数据或 WiFi 时,它会崩溃。我可以使用 WiFi 将数据保存到本地主机上的 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();
}
}

最佳答案

实际上,您不应该在 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();
}
}

如您所见,当我想使用 View (如 TextView )时,我刚刚返回到 UI 线程。关注user多变的。我最终使它能够在 Runnable 内访问。我用作runOnUiThread的参数的对象方法。

关于java - 使用 JSON 和 MySql 使用 Android 应用程序检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33197235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com