2015-10-18 110 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对象中可用。

相关问题