2013-02-15 92 views
0

在向Google Places創建HttpRequest之前,我需要檢查連接。Android:onResume()在HttpRequest上崩潰Google地方

如果沒有活動連接,它將顯示「無連接」給用戶,否則它將獲得當前位置並執行請求。

一些測試的情況:

一個)1.啓用連接 - >獲取數據細,2.再次 - >獲取數據細

B)1.無連接 - > 「無連接」,2 。啓用連接 - >獲取數據精細

C)1.啓用連接 - >獲取數據精細,2.禁用連接 - > 「無連接」

好爲止,但隨後

d)1.活動連接 - >獲取數據罰款,2.禁用連接 - >「無連接」,3.啓用連接 - >「寫入錯誤:ssl = 0x5a90fbb0:系統調用期間的I/O錯誤,損壞的管道」

我有日誌語句確認位置數據在測試用例d)第3步中的HttpRequest之前是正確的。所以我看不出它失敗的原因。根據要求

代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.results); 

// set up location manager to work with location service 
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

if (checkInternet()) { 

    currentLocation = locationManager 
       .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

    location = currentLocation.getLatitude() + "," 
        + currentLocation.getLongitude(); 

    places = new PlaceList(); 

    // keyword is passed in from previous activity 
    Intent intent = this.getIntent(); 
     keyword = intent.getStringExtra("keyword"); 

     if (keyword != "") { 

      class GoogleRequest extends AsyncTask<Void, Void, Integer> { 

       protected Integer doInBackground(Void... params) { 
        mht = MyHttpTransport.createRequestFactory(); 
        Resources res = getResources(); 
        try { 
         HttpRequest request = mht 
           .buildGetRequest(new GenericUrl(
             res.getString(R.string.places_search_url))); 
         request.getUrl().put("key", 
           res.getString(R.string.google_places_key)); 
         request.getUrl().put("location", location); 
         request.getUrl().put("radius", Config.RADIUS); 
         request.getUrl().put("keyword", keyword); 
         request.getUrl().put("sensor", false); 

         places = request.execute().parseAs(
           PlaceList.class); 

           } catch (IOException e) { 
         Log.e(Config.MSGTAG, e.getMessage()); 
        } 

        return null; 
       } 

       @Override 
       protected void onPostExecute(Integer result) { 
     // details omitted because it fails to 
     // get data from the places field 
       } 

      } 

@Override 
protected void onResume() { 
    super.onResume(); 

    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 5000, 100, this); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    locationManager.removeUpdates(this); 
} 

@Override 
public void onLocationChanged(Location location) { 
    locationManager.removeUpdates(this); 
    currentLocation = location; 
} 

public boolean checkInternet() { 
    ConnectivityManager cm = (ConnectivityManager) this 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    NetworkInfo mobile = cm 
      .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    if (wifi.isConnected()) { 
     return true; 
    } else if (mobile.isConnected()) { 
     return true; 
    } 
    return false; 
    } 
} 

登錄:

02-15 19:34:32.582: E/*(9967): Write error: ssl=0x5efd0a98: I/O error during system call, Broken pipe 

日誌的其餘部分是postExecute(),但是那是無關緊要的。

+2

發佈您的代碼以及logcat中的確切崩潰跟蹤。另外,您不能在onResume中發出HTTP請求,它會拋出異常,因爲您會阻止UI線程。你需要在AsyncTask或線程中完成。 – 2013-02-15 19:13:14

+0

我已經添加了代碼@GabeSechan - 我使用AsyncTask。 – 2013-02-15 19:31:23

+0

我還添加了相關日誌@GabeSechan – 2013-02-15 19:35:52

回答

0

我已經解決了這個問題。

在我的自定義HttpTransport類中,我有一個finalApacheHttpTransport對象,每次調用createRequestFactory()方法時都會重新使用該對象。問題似乎是,當連接丟失,然後創建一個新的連接時,它會嘗試使用舊連接(沿着這些連線)。爲了解決這個問題,我在createRequestFactory()方法中創建了一個新的ApacheHttpTransport對象。