How to Determine if an HttpConnection is broken using RestAdapter

Hi,
I have the following piece of code :
RestClient.java :
package co.riva.vader;

import android.util.Log;

import com.google.gson.GsonBuilder;
import com.squareup.okhttp.OkHttpClient;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;

public class RestClient<T> {
    private static final String LOGTAG = RestClient.class.getSimpleName();
    private static final int TIMEOUT_VALUE_IN_SECONDS = 10;

    private T _api;

    public RestClient(String endPoint, Class<T> apiClass, Executor callbackExecutor) {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setConnectTimeout(TIMEOUT_VALUE_IN_SECONDS, TimeUnit.SECONDS);
        okHttpClient.setReadTimeout(TIMEOUT_VALUE_IN_SECONDS, TimeUnit.SECONDS);
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(endPoint)
                .setClient(new OkClient(okHttpClient)).setRequestInterceptor(new RequestInterceptor() {
                    @Override
                    public void intercept(RequestFacade requestFacade) {
                    }
                }).setConverter(new GsonConverter(new GsonBuilder().create()))
                .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new RestAdapter.Log() {
                    @Override
                    public void log(String s) {
                        Log.d(LOGTAG, s);
                    }
                }).setExecutors(Executors.newCachedThreadPool(), callbackExecutor).build();
        _api = restAdapter.create(apiClass);
    }

    public RestClient(String endPoint, Class<T> apiClass) {
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setConnectTimeout(TIMEOUT_VALUE_IN_SECONDS, TimeUnit.SECONDS);
        okHttpClient.setReadTimeout(TIMEOUT_VALUE_IN_SECONDS, TimeUnit.SECONDS);
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(endPoint)
                .setClient(new OkClient(okHttpClient)).setRequestInterceptor(new RequestInterceptor() {
                    @Override
                    public void intercept(RequestFacade requestFacade) {
                    }
                }).setConverter(new GsonConverter(new GsonBuilder().create()))
                .setLogLevel(RestAdapter.LogLevel.FULL).setLog(new RestAdapter.Log() {
                    @Override
                    public void log(String s) {
                        Log.d(LOGTAG, s);
                    }
                }).build();
        _api = restAdapter.create(apiClass);
    }

    public T getApi() {
        return _api;
    }
}

Open in new window



 SuggestionClient.java :

package co.riva.vader;

import java.util.List;

import retrofit.http.GET;
import retrofit.http.Query;

public class SuggestionClient {
    private static final String ENDPOINT = "http://talk-to-kbb-614344748.us-west-1.elb.amazonaws.com";
    SuggestionApi _api;

    public SuggestionClient() {
        RestClient<SuggestionApi> restClient = new RestClient<>(ENDPOINT, SuggestionApi.class);
        _api = restClient.getApi();
    }

    public List<String> getSuggestions(String word, int count) {
        return _api.getSuggestions(word, count);
    }

    public interface SuggestionApi {
        @GET("/sug")
        List<String> getSuggestions(@Query("command") String word, @Query("nResults") int count);
    }
}

Open in new window


Now in my another class i create an object like :
    private SuggestionClient _suggestionClient = new SuggestionClient();
and then later make a call :
_suggestionClient.getSuggestions(constraint.toString(), 4);

This will establish an HttpConnection with the server. and this server keeps the connection alive for 10 minuted.
I want to make sure that the connection stays alive for say 30 minutes.... So if the connection gets broken either after 10 minutes or due to some other issue i want to reconnect to the server....

How do i find out if the connection is broken ??
Thanks
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gheistCommented:
You can catch exception that follows disconnect and reconnect. (it is a problem in the framework, it should do it behind the scenes)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
Can you please show in code . how to do it...
gheistCommented:
I do not want to read retrofit documentation. It is your job, sorry.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.