2014-04-24 30 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