2014-04-24 64 views
0

我想在我的Android應用上使用Post方法獲取內容,但始終崩潰response.getEntity()拋出錯誤「內容已被佔用」。我不知道什麼是錯的。如果我嘗試多次獲取內容,則會出現該錯誤,但在我的代碼中,我只嘗試一次。IllegalStateException:使用httpPost從服務器獲取數據的內容

HttpPost代碼:

公共類MyActivity延伸活動{

/*****************************************/ 
/*** FUNCION POST - RECUPERAMOS DATOS ***/ 
/*****************************************/ 
public class TaskGet extends AsyncTask<String, Integer, String> { 

    TaskGet obj = this; 
    BufferedReader in = null; 
    String stringifiedResponse; 

    JSONObject data = null; 

    public TaskGet() { 
     super(); 
    } 


    @Override 
    protected String doInBackground(String... arg0) { 


      Log.i(getClass().getSimpleName(), "GET task - start"); 

    try { 

     String url = "http://XXXXXX.com/xxxxxx";      
     HttpPost httpRequestHistory = new HttpPost(url); 

     String auth = "xxxxxxxx";     
     httpRequestHistory.addHeader("Authorization", "Basic " + auth); 

     HttpClient httpclient = new DefaultHttpClient(); 

     // LOG 
     Log.i(getClass().getSimpleName(), "called GET HISTORY"); 

     // PARAMETROS 
     String oInt = "10"; 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

     nameValuePairs.add(new BasicNameValuePair("startDate","1393445183")); 
     nameValuePairs.add(new BasicNameValuePair("endDate","1393445198")); 
     nameValuePairs.add(new BasicNameValuePair("filterNumber", oInt)); 

     httpRequestHistory.setEntity(new UrlEncodedFormEntity(nameValuePairs));    

     HttpResponse response = httpclient.execute(httpRequestHistory); 


     StatusLine statusLine = response.getStatusLine(); 
     int statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { 
      String responseHistory = EntityUtils.toString(response.getEntity()); 
      Log.i(getClass().getSimpleName(), responseHistory); 
     } 
     Log.i(getClass().getSimpleName(), "GET task - end"); 
     return null; 

    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 


     } 

    protected void onPostExecute(String result) { 
     stringifiedResponse = result; 
    } 


} 

}

感謝。

回答

0

通過您的建議做這樣的方式::

HttpEntity resEntity = responsePOST.getEntity(); 
if (statusCode == 200) { 
      String responseHistory = EntityUtils.toString(resEntity); 
      Log.i(getClass().getSimpleName(), responseHistory); 
     } 
+0

感謝。它會拋出同樣的錯誤:( – n4gash

+0

@ n4gash你可以發佈所有的代碼?? –

+0

我剛剛添加了所有代碼,謝謝 – n4gash