1

I would like to send a param in POST by using RetroFit, but impossible to get a solution.

I just want to pass an URL, with params with a key and a value, and get JSON answer of type

{"result":[{"id":196,"CREATION_DATE":"2020-01-22T14:33:49.000Z"}]}  

Here is my code Retrofit:

public interface RetrofitInterface {

    @Headers({"Content-type: application/json" , "Accept : application/json"})
    @Streaming
    @POST("https://xxx.newtotelapps.fr:5000/device/listEvent")
    Call<JSONObject> getListEvent(@Body JSONObject jsonObject);
}

My main code:

                JSONObject params2 = new JSONObject();
                try {
                    params2.put("CODE_USER", UserCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                CookieJar cookieJar2 = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(getApplicationContext()));

                OkHttpClient client2 = new OkHttpClient.Builder()
                        .cookieJar(cookieJar2)
                        .build();

                Retrofit retrofit3 = new Retrofit.Builder()
                        .baseUrl("https://xxx.newtotelapps.fr:5000/device/listEvent/")
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                RetrofitInterface downloadService2 = retrofit3.create(RetrofitInterface.class);

                Call<JSONObject> call2 = downloadService2.getListEvent(params2);

                call2.enqueue(new Callback<JSONObject>() {
                        @Override
                        public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
                            //displaying the message from the response as toast


                            System.out.println("test ");

                        }

                    @Override
                    public void onFailure(Call<JSONObject> call, Throwable t){
                    System.out.println(t.toString());
                }
                });

I have this error message : java.lang.IllegalArgumentException: Unexpected char 0x20 at 6 in header name: Accept

EDIT: I suppress the space between "Accept" and ":" and I have that error message now : java.io.IOException: unexpected end of stream on https://xxx.newtotelapps.fr:5000/...

ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • check this answer: https://stackoverflow.com/questions/43026163/unexpected-char-0x0a-in-header-value-when-using-okhttp-client-in-android – Azhagthott Feb 11 '20 at 10:43
  • btw, in POST and in baseUrl you have the same data, its suppoes baseUrl should be https://xxx.newtotelapps.fr:5000 and POST("/device/listEvent") – Azhagthott Feb 11 '20 at 10:45
  • https://stackoverflow.com/a/53838677/2692102 – Hanzala Feb 11 '20 at 10:46
  • see this thread as well https://github.com/square/retrofit/issues/2518#issuecomment-335246288 – Hanzala Feb 11 '20 at 10:56
  • 2
    Below answer was correct. Remove space between `Accept` and `:` in Headers. Exception clearly says 6th character which is space. – Bek Feb 11 '20 at 11:03
  • Yep, I updated my ticket by suppressing that space. I have an error message (java.io.IOException: unexpected end of stream on https://xxx. newtotelapps.fr:5000/...) – ΩlostA Feb 11 '20 at 11:05
  • https://stackoverflow.com/a/42529768/8942811 – Bek Feb 11 '20 at 11:16
  • Remove `@Streaming` – Bek Feb 11 '20 at 11:27
  • @Bek it does the same – ΩlostA Feb 11 '20 at 11:33
  • Why did you use `@Streaming`? As I understand you just want to make post request and receive result once. Am I correct? If so make sure your sending `json` is correct and server is working. Try with postman. – Bek Feb 11 '20 at 11:42
  • Yep exactly. I removed it, I don’t need it you re right, but I have the same error message – ΩlostA Feb 11 '20 at 11:56

1 Answers1

0

EDIT :

0x20 character

The space character, that denotes the space between words, as produced by the space-bar of a keyboard, represented by code 0x20 (hexadecimal), is considered a non-printing graphic (or an invisible graphic) rather than a control character.

Copy the following code:

public interface RetrofitInterface {

@Headers({"Content-type: application/json" , "Accept: application/json"})
@Streaming
@POST("https://xxx.newtotelapps.fr:5000/device/listEvent")
Call<JSONObject> getListEvent(@Body JSONObject jsonObject);

}

you have mistakenly keep space after "Accept :". Do this "Accept:"

Solution for Error: java.io.IOException: unexpected end of stream on https://xxx.newtotelapps.fr:5000/...

Use Google Gson Library, Add it into build.gradle:

dependencies {
    implementation 'com.google.code.gson:gson:2.8.6'
}

Use: import com.google.gson.JsonObject;

Instead of: import org.json.JSONObject;

I hope you will got it.

Else error can occur in 2nd case:

You need to set Connection:close in header.

@Headers({"Content-type: application/json" , "Accept: application/json", "Connection: close"})

Also error can occur in 3rd case:

having server side problem...you need to set header('Content-Length: '.$length); in server side script

Nikesh Nayak
  • 302
  • 2
  • 9
  • Oh yes, you're right! My mistake. Thank you. I have another error message now that is "java.io.IOException: unexpected end of stream on https://xxx. newtotelapps.fr:5000/..." – ΩlostA Feb 11 '20 at 11:08
  • 1
    yes my pleasure wait...i just need to figure out it – Nikesh Nayak Feb 11 '20 at 11:12