2015-07-20 134 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 –

相关问题