2015-12-17 53 views
-1

在我的Android應用程序中,我試圖發送一個json對象到遠處的服務器,當我運行它時,我在httpclient.execute(httpPost)中得到一個錯誤 這是我的代碼的一部分。httpclient.execute(httpPost)錯誤

public static String GET(String url , JSONObject js){ 

    try { 
     HttpPost httpPost = new HttpPost(url);   

     httpPost.addHeader("Authorization", "Basic **********"); 
     httpPost.setEntity(new StringEntity(js.toString())); 

     HttpClient httpclient = new DefaultHttpClient();    
     httpclient.execute(httpPost); 
    } catch (Exception e) { 
     Log.i("Console", "Error"); 
    } 

請任何幫助。

+2

什麼樣的錯誤?發佈你的堆棧跟蹤...也許在'catch'子句中添加'e.printStackTrace()' – LordRaydenMK

+0

我敢打賭,錯誤就像「NetworkOnMainThreadException」:) –

回答

1

我想你的問題是,你嘗試從你的主線程運行你的網絡請求。

我會勸阻你使用Apache HTTP客戶端。

它被廢棄了棉花糖,看到here

也許嘗試OkHttp。它爲您提供了異步運行請求的可能性。

相關問題