2015-11-06 26 views
-2

我有这个功能,它执行网络操作..(即将数据发布到网页中)..但我得到android.os.NetworkOnMainThreadException ..我知道我必须使用android.os.NetworkOnMainThreadException在网络上的线程操作,但我不知道该怎么做......难道u人请帮我看看我的代码..提前 感谢..... 这里是我的方法如何使用线程进行联机操作

 public static String excutePost(String targetURL, String urlParameters) 
{ 
    URL url; 
    HttpURLConnection connection = null; 
    try { 


    //Create connection 
     url = new URL(targetURL); 
     connection = (HttpURLConnection)url.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Content-Type", 
      "application/x-www-form-urlencoded"); 

     connection.setRequestProperty("Content-Length", "" + 
       Integer.toString(urlParameters.getBytes().length)); 
     connection.setRequestProperty("Content-Language", "en-US"); 

     connection.setUseCaches (false); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 

     //Send request 
     DataOutputStream wr = new DataOutputStream (
        connection.getOutputStream()); 
     wr.writeBytes (urlParameters); 
     wr.flush(); 
     wr.close(); 

     //Get Response  
     InputStream is = connection.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     while((line = rd.readLine()) != null) { 
     response.append(line); 
     response.append('\r'); 
     } 
     rd.close(); 
     return response.toString(); 

    } catch (Exception e) { 

    String g=e.toString(); 
    Log.d("errererer", g); 
    return null; 

    } finally { 

    if(connection != null) { 
     connection.disconnect(); 
    } 
    } 
} 
+0

可以使用的AsyncTask了点。检查此链接http://developer.android.com/intl/zh-tw/reference/android/os/AsyncTask.html – DarwinLouis

回答

0
private class AsyncCaller extends AsyncTask<Void, Void, Void> 
{ 
ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this); 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 

    //this method will be running on UI thread 
    pdLoading.setMessage("\tLoading..."); 
    pdLoading.show(); 
} 
@Override 
protected Void doInBackground(Void... params) { 

    //this method will be running on background thread so don't update UI  
     frome here 
    //do your long running http tasks here,you dont want to pass argument 
    //and u can access the parent class' variable url over here 
    return null; 
} 

@Override 
protected void onPostExecute(Void result) { 
    super.onPostExecute(result); 

    //this method will be running on UI thread 

    pdLoading.dismiss(); 
} 

} 
} 

,并在OnCreate()调用这个AS-

new AsyncCaller().execute(); 
0

使用线程或Asynctask

调用asynctask

new TheTask().execute("http://192.168.1.88/androidtesting.php"); 

的AsyncTask

http://developer.android.com/reference/android/os/AsyncTask.html

class TheTask extends AsyncTask<String,String,String> 
    { 

@Override 
protected String onPostExecute(Void result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 
    // update textview here 
    textView.setText("Server message is "+result); 
} 

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
} 

@Override 
protected String doInBackground(String... params) { 
    try 
     { 
      excutePost(String targetURL, String urlParameters); 
     } 
     catch(Exception e){ 
      return "Network problem"; 
     } 

} 
} 
+0

谢谢你... :)这有助于...... – Benedict

+0

Oooohhh ......快乐亲爱的...... :) –