2015-10-18 33 views
0

我試圖使用WiFi回顧數據,但應用程序崩潰。當我使用localHost模擬器時,它工作得很好,但是當我使用移動數據或WiFi時,它會崩潰。我可以使用WiFi將數據保存到本地主機上的MySql,但我不能接收數據。有些時候我得到android.os.NetworkonMainThreadException。我是一個非常新的程序員,只是借用了大部分代碼,並且需要清楚地說明如何解決這個問題。使用JSON和MySql使用Android應用程序回收數據

/** 
* 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(); 
    } 
} 

回答

2

其實你不應該連接到互聯網的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(); 
} 
} 

正如你可以看到我剛回到UI線程,當我想處理視圖(如文本視圖)。注意user變量。我做了最後的處理,使其在作爲runOnUiThread方法參數的Runnable對象中可用。

相關問題