0

Before making an HttpRequest to Google Places, I need to check for a connection.

If there is no active connection, it will display "No Connection" to the user, otherwise it will get the current location and perform the request.

Some test cases:

a) 1. Enable connection -> Gets data fine, 2. Again -> Gets data fine

b) 1. No connection -> "No Connection", 2. Enable connection -> Gets data fine

c) 1. Enable connection -> Gets data fine, 2. Disable connection -> "No Connection"

Good so far, but then

d) 1. Active Connection -> Gets data fine, 2. Disable connection -> "No Connection", 3. Enable Connection -> "Write error: ssl=0x5a90fbb0: I/O error during system call, Broken pipe"

I have log statements confirming that the location data is correct before the HttpRequest in the 3rd step of test case d). So I cannot see any reason why it is failing.

Code as requested:

@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;
    }
}

Log:

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

the rest of the log is the postExecute() but that's irrelevant.

4

1 回答 1

0

我已经解决了这个问题。

在我的自定义HttpTransport类中,我有一个final ApacheHttpTransport对象,每次createRequestFactory()调用方法时都会重新使用它。问题似乎是,当连接丢失,然后创建一个新连接时,它会尝试使用旧连接(类似这些线)。所以为了解决这个问题,我ApacheHttpTransport在方法中创建了一个新对象createRequestFactory()

于 2013-02-16T01:02:06.707 回答