2011-11-08 58 views
0

我的Android應用程序出現問題。我的意思是我使用的代碼在互聯網TUTS之一,看起來像下面Android httppost網絡違規

public static String sendLocation(String s) { 
    // Create a new HttpClient and Post Header 
    //HttpParams params = new BasicHttpParams(); 
    //params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://google.com/read.php"); 
    //String d = "ok"; 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("user", "mobile")); 
     nameValuePairs.add(new BasicNameValuePair("location", s)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

    } catch (ClientProtocolException e) { 
     // d = "CPE"; 
    } catch (IOException e) { 
     // d = "E"; 
    } 
    return "send"; 
} 

}`

而且問題/錯誤出現,當應用程序試圖將數據發送到遠程服務器(線下)

// Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost);

我還使用StrictMode,之後一切正常,日誌顯示網絡侵犯

11-08 22:34:40.020:D/StrictMode(939):StrictMode策略違規; 〜持續時間= 359毫秒:android.os.StrictMode $ StrictModeNetworkViolation:政策= 31違反= 4

我列入清單文件適當的權限

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

你有什麼想法,我應該怎麼辦運行我的應用程序(實際上是通過郵件發送數據到服務器)沒有StrictMode?

回答

1

問題是您正嘗試在主UI線程上執行網絡調用。只能在單獨的線程中使用這些類型的方法。

的一個很好的例子是在這裏:http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

+0

什麼是 「政策= 31違反= 4」 是什麼意思?是在哪裏表的政策和違規列表? 31和4是什麼意思?,謝謝 – Lukap

+0

請參閱[這個問題](http://stackoverflow.com/a/10955043/2050)關於如何找到有關政策和違規行爲。 –