2014-02-09 21 views
-1

和錯誤的是:HttpClient.execute誤差=(

錯誤打開跟蹤文件:沒有這樣的文件或目錄(2); java.lang.IllegalStateException:管理器關閉在 org.apache .http.impl.conn.SingleClientConnManager.assertStillUp(SingleClientConnManager.java:174) org.apache.http.impl.conn.SingleClientConnManager.getConnection(BLA 血乳酸血乳酸的.java:212) org.apache.http.impl。 conn.SingleClientConnManager $ 1.getConnection(---。java:190) org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:326) org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) org.apache.http.impl.client.AbstractHttpClient.xecute(AbstractHttpClient.java:487) org.apache.http.impl。 client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

and more... 

public class ReqClass { 
    public static final String TAG = ReqClass.class.getSimpleName(); 
    //creating new ResClass object 
    ResClass resp = new ResClass(); 
    //sending request 
    HttpResponse response; 
    //making the request - containst (url,header,entity); 
    HttpPost request; 
    //New HTTP Client 
    HttpClient httpClient = new DefaultHttpClient(); 

    //our code in JSON or XML to Send Request 
    private String s; 
    //This variable contains the total number of keys 
    private int size; 

    // Map 
    Map<String,String> code = new HashMap<String, String>(); 

    // Adding only Key Value parameters 
    public void addparametr (String key, String value){     
     code.put(key,value); 
    } 

    public String getS(){ 
     return s; 
    } 

    //Adding Key Value and size of new map. 
    public void addparametr (String key, String value, int nOfValues){  
    } 

    public void Build(String lang){ 
     //List Of keys 
     List<String> keys=new ArrayList<String>(code.keySet()); 
     //k = keys; v = values; 
     String k="",v=""; 
     // size = size of Map; 
     size=code.size(); 
     // if we want to make our JSON Code to send Request 
     if (lang=="json"){             
      s=""; 
      s+="{"; 
      for (int i=0; i<size; i++){ 
       k=keys.get(i); 
       v=code.get(k); 
       if (i==size-1) s=s+'"'+k+'"'+": "+'"'+v+'"'; 
       else s=s+'"'+k+'"'+": "+'"'+v+'"'+','; 
      } 

      s=s+"}"; 
     } 

     if (lang =="xml"){             
      s=""; 
      s+="<code>"; 
      for (int i=0; i<size; i++){ 
       k=keys.get(i); 
       v=code.get(k); 
       s=s+'<'+k+'>'+v+"</"+k+'>'; 
      } 
      s=s+"</code>"; 
     } 
    } 

    // The Send Request Method - need link parameter and our JSON or XML code 
    public ResClass SendR (String url, String lang){       
     String valueOfSize = String.valueOf(size); 
     request= new HttpPost(url); 
     //adding a Header 
     request.addHeader("Header - Request Size", valueOfSize); 
     //adding request's body (Entity) 
     HttpEntity entity; 
     try { 
      entity = new StringEntity(s, "UTF-8"); 
      request.setEntity(entity); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     }    
     BackGroundTask doItInBackGround = new BackGroundTask(); 
     doItInBackGround.execute(); 

     //closing our connection 
     httpClient.getConnectionManager().shutdown(); 
     return resp; 
    } 
    private class BackGroundTask extends AsyncTask<Void, Void, Void>{ 

     @Override 
     protected Void doInBackground(Void... params) { 
      try { 
       //execute - means sending 
       response = httpClient.execute(request); 
       //adding new response to our Response Class 
       resp.addNewResponse(response); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (Exception e) { 
       Log.e(TAG, "Exception caught: ", e); 
      } 
      return null; 
     } 
    } 
} 

回答

1

你是剛開始AsyncTask後調用getConnectionManager().shutdown()執行doInBackground方法連接之前,因此被關閉。因此使用onPostExecute進行緊密連接:

protected void onPostExecute(Long result) { 
     // call shutdown() here 
     httpClient.getConnectionManager().shutdown();  
} 
+0

謝謝)它的工作原理)但是...爲什麼我無法從網絡獲得我的迴應? – Raqim

+0

保護無效onPostExecute(空結果){ super.onPostExecute(result); HttpEntity entity = response.getEntity(); String str = entity.toString(); \t \t \t httpClient.getConnectionManager()。shutdown(); Log.v(「Json =」,str); } – Raqim

+0

@ user3289413:嘗試在'onPostExecute' –