2015-07-20 157 views
0

我已經創建了一個類,它從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(); 
    } 
} 
+0

這些是一些錯誤。 E/AndroidRuntime(11784):android.os.NetworkOnMainThreadException android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156) java.net.InetAddress.lookupHostByName(InetAddress.java:385) –

+0

這是不正確的。使用AsyncTack沒有這種意義。您必須在doBackground()方法上執行您的工作,並調用onProgressUpdate()來更新UI。 onProgressUpdate()方法在UI線程上運行。 –

+0

刪除doinbackground中的runnable。你不需要它有 – dreamer1989

回答

0

您的TAG是否包含多個?記錄或單查詢記錄是取每次如果奇異把你按照OnPostExecute()的代碼段,在doInBackground刪除可運行()東陽你的代碼已經在線程..變量

 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)); 

商店中的記錄在doInBackground(),然後傳遞給EDITTEXT對象

String temp=product.getString(TAG_NAME); 
txtName.setText(temp); 

在OnPostMethod()添加

super.onPostExecute(file_url); 

在Manifest.xml文件添加這些行,如果你沒」放在它上面。

<uses-permission android:name="android.permission.INTERNET"/> 

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

最後檢查logcat的是你的JSON被正確解析,然後更新您的問題或答案,不要跳的喜歡和不喜歡第一。

+0

這並不工作, LogCat仍然顯示相同的錯誤 –

+0

http://stackoverflow.com/questions/19941703/android-os-networkonmainthreadexception-at-android-os-strictmodeandroidblockgua檢查此鏈接@Abel Pule Chilunge –

相關問題