2

when using androids URL and HttpUrlConnection to send a GET request to a backend point, it sometimes (1 out of 10) occurs, that the request fails due to: java.net.ProtocolException: Unexpected status line: 1.1 200 OK

As said this only happens somtimes, I tried 3 different backends (one of them self hosted) and it still occurs.

        System.setProperty("http.keepAlive", "false");
        URL url = new URL(callUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setUseCaches(false);
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        con.setConnectTimeout(5000);
        con.setReadTimeout(4000);
        con.setRequestMethod(requestMethod);
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        con.setRequestProperty("Accept-Charset", "UTF-8");
        con.setRequestProperty("charset", "UTF-8");
        con.setRequestProperty("Connection", "close");

Maybe someone has an idea how to fix it?

1 Answers1

0

First of all, this is the API references link: http://square.github.io/retrofit/

So, go to: File > Project Structure , after opened, in modules - app, go to tab Dependencies.

My Project Depedencies

Click on the + symbol and add library

Search and add this library: com.squareup.retrofit2:retrofit:2.30, and I highly recommend you to use Jackson too, if you want it, add this library to: com.squareup.retrofit2:converter-jackson:2.3.0

After all depedencies and building, let's go to the code.

I created a RetrofitInitialization class with this code:

public class RetrofitInicializador {
public RetrofitInitialization() {

String url = "localhost:8080/webservice/";
retrofit = new Retrofit.Builder().baseUrl(url)
                .addConverterFactory(JacksonConverterFactory.create()).build();
}
}

We need to create a service too, so, I created a service class called: ObjectService

public interface ObjectService {

    @POST("post/example")
    Call<Object > postexemple(@Body Object object);

    @GET("get/example/{id}")
    Call<Object> getexemple(@Path("id") Integer id);

}

The Object is your model that you want to receive or send. After this, add your service into the RetrofitInitialization, after the constructor.

Similar this:

public ObjectService getObjectService() {
    return retrofit.create(ObjectService.class);
}

In your Activity or anywhere you want to get this informations, do something like this:

 private void loadFromWS(Object object) {
        Call<Object> call = new RetrofitInicializador().getObjectService().postexemple(object);
        call.enqueue(new Callback<Object>() {
            @Override
            public void onResponse(Call<Object> call, Response<Object> response) {
                Object response = response.body();

               // DO your stuffs
            }

            @Override
            public void onFailure(Call<Object> call, Throwable t) {
                Toast.makeText(AgendaActivity.this, "Connection error", Toast.LENGTH_SHORT).show();
            }
        });
    }

Edit: Forget to tell, I used this on WS REST server (to be more specific: JAX-RS and Jersey WS)

Felipe Junges
  • 180
  • 12