I'm trying to register a user using okHttpClient 4.2.1 library and as a response I'm getting some garbage value
Respone is �������������V*.MNN-.V�JK�)N�Q�r�S���rR�SSR��K2��t�RK���ck�NY�<������
I used postman and the response is what I'm expecting. I don't understand why its not working in Android Studio.
{"result": {"OTP": 1113401845,"id": 143},"success": true,"message": "User Registered In Successfully"}
Here is the code I used
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
JSONObject postdata = new JSONObject();
try {
postdata.put("action", "registrationFirst");
postdata.put("User_name", "abcd");
postdata.put("password", "12345");
postdata.put("email", "[email protected]");
postdata.put("street", "qa");
postdata.put("Gender", "1");
postdata.put("Religion", "3");
postdata.put("caste", "Other");
postdata.put("Country", "UAE");
postdata.put("mobile", "0123456");
postdata.put("Locaion", "SHJ");
} catch(JSONException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
RequestBody body = RequestBody.create(mediaType,postdata.toString());
Request request = new Request.Builder()
.url(myUrl)
.post(body)
.addHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW")
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Basic aW5mb0Bhbm9vbnouY29tOjEyMTIxMg==")
.addHeader("User-Agent", "PostmanRuntime/7.20.1")
.addHeader("Accept", "application/json")
.addHeader("Cache-Control", "no-cache")
.addHeader("Postman-Token", "cf631a82-2570-4c20-9c39-be0563240c67,f9cecf3d-2340-4fce-bb6f-c060c9f8894f")
.addHeader("Accept-Encoding", "gzip, deflate")
.addHeader("Content-Length", "1278")
.addHeader("Connection", "keep-alive")
.addHeader("cache-control", "no-cache")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
String result = response.body().string();
Log.d("Hola","Response is "+result);
}
} catch (Exception e) {
e.printStackTrace();
Log.d("Hola","Failed exception "+e);
}
You should remove the following line when building a request using Request.Builder:
addHeader("Accept-Encoding", "gzip, deflate")
When you specify your own Accept-Encoding value you're implying that you'd like to do your own decompression which is not the case for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With