我已經創建了一個類,它從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