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/...