我已经创建了一个类,它从MySQL数据库中检索数据并将其显示到各种编辑文本中,如下所示。在本地主机上测试应用程序时,它正在通过PHP文件从JSON数组中检索数据,但是当我将HTTPRequest的URL从本地主机http://10.0.2.2/concurency/get_user_details_test.php更改为远程主机http://concurrenceypule.netau.net/get_user_details_test.php时,运行在V 2.3上的应用程序,正在粉碎。所有关于远程数据库连接的信息都在正常工作,因为它正在通过Web浏览器正确显示数据。 什么可能导致应用程序粉碎,以及如何解决。在android应用程序中的远程服务器连接
private static final String url_product_detials = "http://concurrenceypule.netau.net/get_user_details_test.php";
class GetProductDetails extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
final String user_name2 = "paul";
runOnUiThread(new Runnable() {
public void run() {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_name",user_name2));
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
Log.d("Single Product Details", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT);
JSONObject product = productObj.getJSONObject(0);
txtName = (EditText) findViewById(R.id.username);
txtcontact = (EditText) findViewById(R.id.tv_contact);
txtemail = (EditText) findViewById(R.id.tv_email);
txtaddress = (EditText) findViewById(R.id.et_Address);
txtName.setText(product.getString(TAG_NAME));
txtcontact.setText(product.getString(TAG_contact));
txtemail.setText(product.getString(TAG_email));
txtaddress.setText(product.getString(TAG_address));
}else{
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
这些是一些错误。 E/AndroidRuntime(11784):android.os.NetworkOnMainThreadException android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156) java.net.InetAddress.lookupHostByName(InetAddress.java:385) –
这是不正确的。使用AsyncTack没有这种意义。您必须在doBackground()方法上执行您的工作,并调用onProgressUpdate()来更新UI。 onProgressUpdate()方法在UI线程上运行。 –
删除doinbackground中的runnable。你不需要它有 – dreamer1989