2013-10-07 32 views
0

我正在嘗試使用Apache HTTPClient執行一個簡單的獲取請求,但它好像在HTTPResponse響應= client.execute(get)之後的所有代碼似乎;正在被跳過。我無法訪問響應對象的內容,它們都是空的。但是,當我使用調試模式,並探索對象,我看到所有的數據。這個函數被包裝在一個異步任務中,所以我想知道任務本身並沒有等待它被執行或者我不確定。Apache HTTPGet在Android中表現怪異

同樣的事情也發生在這裏: Android code after httpclient.execute(httpget) doesn't get run in try (using AsyncTask)

這裏是代碼。

@Override 
public T execute() 
{ 
    utils = new GeneralUtils(); 
    if(getURL() == null) 
    { 
     throw new NullPointerException("No path specified"); 
    } 
    HttpClient client = new DefaultHttpClient(); 
    HttpGet get = new HttpGet(getURL()); 

    Log.e(TAG,"client created"); 
    if(getHeaders()!=null) 
    { 
     Log.e(TAG,"client created"); 
     for(Map.Entry<String,String> header:getHeaders().entrySet()) 
     { 
      get.addHeader(header.getKey(),header.getValue()); 
     } 
    } 

    try 
    { 
     HttpResponse response = client.execute(get); 
     Log.e(TAG,"executed"); 

     if(response==null) 
      Log.v(TAG,"its null as heell"); 

     Log.v(TAG,response.getStatusLine().getReasonPhrase()); 
     Log.v(TAG,String.valueOf(response.getStatusLine().getStatusCode())); 
     Log.e(TAG,getURL()); 

     Log.v(TAG,"everything else is dead"); 

     for(Header header:response.getAllHeaders()) 
     { 
      Log.v(TAG,header.getName()+" "+header.getValue()); 
     } 

     if(response.getStatusLine().getStatusCode() == 200) 
     { 
      if(response.getEntity().getContent()!=null) 
      { 

       try 
       { 
        if(utils.isExternalStorageWritable()) 
        { 
         String path = getContext().getFilesDir().getAbsolutePath()+"/"+getFileCategory()+"/" +getAlarmId()+getFileExtension(); 
         media = new File(path); 

         /** 
         * if the directory has not being created this function does the creation. 
         */ 

         media.mkdirs(); 

         FileOutputStream fileOutputStream = new FileOutputStream(media); 
         IOUtils.copy(response.getEntity().getContent(),fileOutputStream); 
         fileOutputStream.close(); 

         Log.e(TAG,media.getAbsolutePath()); 

         return (T)media; 

        } 
        return null; 


       } 
       catch (ClientProtocolException e) 
       { 
        Log.v(TAG,e.getMessage()); 
       } 
       catch (IOException e) 
       { 
        Log.v(TAG,e.getMessage()); 
       } 
      } 

     } 
    } 
    catch (IOException e) 
    { 
     Log.e(TAG, e.getCause().getMessage()); 
     e.printStackTrace(); 
    } 

    return null; 
} 

代碼沒有拋出任何異常,所以我不知道發生了什麼。 響應對象之後的所有代碼都不起作用。它只是返回null,因爲只要我嘗試從response.getStatusCode()這樣的響應中獲得一個值,就好像代碼已經死了,而且只是返回null。

回答

0

爲什麼不使用能處理所有這些寧靜連接的庫?

我會建議夫婦:

https://github.com/darko1002001/android-rest-client(這是我的我必須先提到它:)。我爲我建立的項目建立了這個庫。爲了您的情況,您需要提供一個解析器,它將爲您提供一個InputStream,您將其保存爲一個文件(就像現在使用IO util進行操作一樣)。它處理整個事物的異步部分,並且通常給你一個組織代碼的好方法。

http://square.github.io/retrofit/ - 是我一直在玩的另一個。我認爲它做得很好,應該可以隨心所欲地做任何事情。

http://java.dzone.com/articles/android-%E2%80%93-volley-library - Volley是一個直接從Google發佈的項目,它在最後一次Google IO會議上演示。它也爲您處理所有的異步操作,並使您能夠完成所有這些操作。我不確定的一件事是它是否能夠讓你在後臺線程中解析響應。

我強烈建議您使用其中之一,因爲它們可能會爲您節省大量時間。 如果你確實想繼續你的代碼,那麼我會建議先調查一下你是否跳過了某些「if」塊,使用調試器或添加日誌消息來查看它是否進入塊。一步一步看看出了什麼問題。 我在做我的項目類似的東西,看看這個文件: https://github.com/darko1002001/android-rest-client/blob/master/android-rest-lib/src/main/java/com/dg/libs/rest/client/BaseRestClient.java

+0

我只是試圖使用廣場的Okhttp庫和發生同樣的問題。這真的很奇怪。我不確定是什麼造成了這一點。 –

+0

它會拋出異常嗎?如果它只是跳過它下面的所有內容,就一定會有例外。你在日誌中得到了什麼?你試圖打的網址是什麼? – DArkO