I'm using retrofit and I need to upload and image, but I'm getting status code 400.
Here's the code.
This is the interface.
public interface SupportInterface {
//Get request for sending photo in chat
#Multipart
#POST("/api/upload-image")
Call<ResponseBody> getChatPhoto(#Header("Content-Type") String json,
#Header("Authorization") String token,
#Header("Cache-Control") String cache,
#Part("type") String type,
#Part("user_id") String userId,
#Part MultipartBody.Part image_path);
}
I'm using headers + I need to send user_id and type too. So I'm using #Part. I done right yes?
Here's the retrofit in initialization part.
public class ApiClient {
private static ApiClient instance;
OkHttpClient.Builder client = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS);
client.addInterceptor(new Interceptor() {
#Override
public Response intercept(#NonNull Chain chain) throws IOException {
Request request = chain.request();
request = request.newBuilder()
.header("Cache-Control", "public, max-age=0")
.build();
return chain.proceed(request);
}
});
supportopApi = new Retrofit.Builder()
.baseUrl(endpoint)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(SupportopApi.class);
}
public Call<ResponseBody> getChatImage(MultipartBody.Part multipartBody) {
return supportopApi.getChatPhoto("application/json", There is my accessToken,
"no-cache", "5", This is userID, multipartBody);
}
}
If I done here something wrong please tell me.
And here's the main part.
public void getChatImage() {
File file = new File("/storage/emulated/0/Download/s-l640.jpg");
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part multiPartFile = MultipartBody.Part.createFormData("image", file.getName(), reqFile);
Call<ResponseBody> chatImageCall = apiClient.getChatImage(multiPartFile);
chatImageCall.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
Log.d(TAG, response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(context, "Response is not successful: " + response.errorBody(), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "An error occurred", Toast.LENGTH_SHORT).show();
}
});
}
I'm getting here bad request 400.
#Header("Content-Type") String json
You declare your content type is JSON but actually you pass to server the multipart (form data)
So I think you're trying to do:
#Header("Accept") String json
(accept JSON from server)
Related
I'm using an asynchronous request (because synchronous doesn't work correctly with this API), the OkHttp3 library. Inside the request, I get a response in the form of JSON. I need to somehow pass values from Json to a class variable. I decided to try it this way, but it doesn't work.
public String sessionId = null;
...
public QRcodeReader() throws Exception {
this.sessionId = null;
}
... // between this code I have 1 function, which reg my number.
// in this func I need to confirm my phone by code in SMS.
public void SmsCode(String Code) // String get from another class
{
SmsJson smsJson = new SmsJson("*phoneNumber*", "*secret_key*", "*os*", Code);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
RequestBody body = RequestBody.create(
MediaType.parse("application/json"), gson.toJson(smsJson));
Request request = new Request.Builder()
.url("https://irkkt-mobile.nalog.ru:8888/v2/auth/phone/verify")
.addHeader("Host", HOST)
.addHeader("Accept", ACCEPT)
.addHeader("Device-OS", DEVICE_OS)
.addHeader("Device-ID", DEVICE_ID)
.addHeader("clientVersion", CLIENT_VERSION)
.addHeader("Accept-Language", ACCEPT_LANGUAGE)
.addHeader("User-Agent", USER_AGENT)
.post(body)
.build();
httpClient.newCall(request).enqueue(new Callback() {
#Override
public void onResponse(#NonNull Call call, #NonNull Response response) throws IOException {
try (Response responseBody = httpClient.newCall(request).execute()) {
if (!responseBody.isSuccessful())
throw new IOException("Unexpected code " + responseBody);
// Here I try to transfer data from request to class variable. Not Work.
SetSessionId(Objects.requireNonNull(responseBody.body()).string());
System.out.println(Objects.requireNonNull(responseBody.body()).string());
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
});
}
void SetSessionId(String sessionId){
this.sessionId = sessionId;
}
I need to use SessionID further along here:
public String GetTicketID(String QR){
TicketID ticketID = new TicketID(QR);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
RequestBody body = RequestBody.create(
MediaType.parse("application/json"), gson.toJson(ticketID));
Request request = new Request.Builder()
.url("https://irkkt-mobile.nalog.ru:8888/v2/ticket")
.addHeader("Host", HOST)
.addHeader("Accept", ACCEPT)
.addHeader("Device-OS", DEVICE_OS)
.addHeader("Device-ID", DEVICE_ID)
.addHeader("clientVersion", CLIENT_VERSION)
.addHeader("Accept-Language", ACCEPT_LANGUAGE)
.addHeader("User-Agent", USER_AGENT)
.addHeader("sessionId", sessionId) // Here I get NULL and exception!
.post(body)
.build();
httpClient.newCall(request).enqueue(new Callback() {
#Override
public void onResponse(Call call, Response response) throws IOException {
try (Response responseBody = httpClient.newCall(request).execute()) {
if (!responseBody.isSuccessful())
throw new IOException("Unexpected code " + responseBody);
System.out.println(Objects.requireNonNull(responseBody.body()).string());
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
});
return ""; // Here I need to transfer id to another variable, not did it yet.
}
The problem is that SessionID = Null and the request does not work.
I think I'm doing something wrong, but I can't figure out what. May be it is because async request..
currently working on an Android App and I'm supposed to send JSON data to a server and receive back a response, but the server doesn't recognize the data frame as a JSON type it still shows its content but replies with a 404 code
here's the code I used :
private final OkHttpClient client = new OkHttpClient();
JSONObject jsonObj = new JSONObject();
try{
jsonObj.put("imei",number);
jsonObj.put("longitude", number);
jsonObj.put("latitude", number);
jsonObj.put("speed", number);
jsonObj.put("battery", number);
jsonObj.put("date", date);
jsonObj.put("error", number);
jsonObj.put("acceleroVal", number);
jsonObj.put("apn", string);
} catch (JSONException e) {
e.printStackTrace();
}
String json = jsonObj.toString();
RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
RequestBody formBody = new FormBody.Builder()
.build();
Request request = new Request.Builder()
.url("http://xxxx.com:2000/api/xxx")
.put(formBody)
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(#NonNull Call call, #NonNull IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(#NonNull Call call, #NonNull Response response) throws IOException {
if(response.isSuccessful()) {
ResponseBody responseBody = response.body();
}
any help is appreciated, feel free to adjust the code.
I'm using Retrofit and OkHttp in my Project and i want to refresh token when the server gives 401 error code and store the new token in shared preference and make the same call again without notifing the user. Token type is bearer and expires every hour.
But the output that i desired is not coming, when the token expires the authenticator does nothing.
ApiInterface.java
public interface ApiInterface {
#FormUrlEncoded
#POST("auth/login")
Call<ResponseBody> login(#FieldMap HashMap<String, Object> map);
#FormUrlEncoded
#POST("auth/register")
Call<ResponseBody> register(#FieldMap HashMap<String, Object> map);
#FormUrlEncoded
#POST("auth/forgotpassword")
Call<ResponseBody> forgotPassword(#FieldMap HashMap<String, Object> map);
#POST("auth/refresh")
Call<ResponseBody> refreshToken(#Header("Authorization") String token);
#POST("auth/GetAllInterests")
Call<ResponseBody> getAllInterest();
#POST("auth/AddUserProfile")
#Multipart
Call<ResponseBody> addUserProfile(
#Part("display_name") RequestBody display_name,
#Part("children_age_show") RequestBody children_age_show,
#Part("address") RequestBody address,
#Part("no_of_children") RequestBody no_of_children,
#Part("children_age") RequestBody children_age,
#Part("date_of_birth") RequestBody date_of_birth,
#Part("interests") RequestBody interests,
#Part List<MultipartBody.Part> images
);
#POST("auth/GetUserProfile")
Call<ResponseBody> getUserProfile();
#FormUrlEncoded
#POST("auth/ChangePassword")
Call<ResponseBody> changePassword(#FieldMap HashMap<String, Object> map);
#POST("auth/logout")
Call<ResponseBody> logout();
}
ApiClient.java
public class ApiClient {
private static Retrofit retrofit = null;
private static ApiInterface apiInterface = null;
public static Retrofit getRetrofit(Context context) {
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (AppPreference.getPreference(context, AppPersistence.keys.AUTH_TOKEN) != null) {
request = request.newBuilder()
.addHeader("Authorization", AppPreference.getPreference(context, AppPersistence.keys.AUTH_TOKEN))
.build();
}
return chain.proceed(request);
}
})
.authenticator(new TokenAuthenticator(context))
.build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofit;
}
public static ApiInterface getApiInterface(Context context) {
if (apiInterface == null) {
getRetrofit(context);
apiInterface = retrofit.create(ApiInterface.class);
}
return apiInterface;
}
}
TokenAuthenticator.java
public class TokenAuthenticator implements Authenticator {
Context context;
public TokenAuthenticator(Context context) {
this.context = context;
}
#Override
public Request authenticate(Route route, Response response) throws IOException {
Log.d("Authenticator", "Authenticator Called");
if (response.code() == 401) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Log.d("Authenticator", "Requesting for New Token");
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
retrofit2.Response<ResponseBody> response1 = apiInterface
.refreshToken(AppPreference.getPreference(context, AppPersistence.keys.AUTH_TOKEN))
.execute();
if (response1.isSuccessful()) {
try {
String body = response1.body().string();
JSONObject jsonObject = new JSONObject(body);
if (jsonObject.getBoolean("status")) {
String newToken = "Bearer " + jsonObject.getJSONObject("Data").getString("access_token");
AppPreference.setPreference(context, AppPersistence.keys.AUTH_TOKEN, newToken);
Log.d("Authenticator", "New Token Generated");
return response.request().newBuilder()
.header("Authorization", newToken)
.build();
}
} catch (Exception e) {
Log.e("TokenAuthenticator", e.getMessage(), e);
}
}
Toast.makeText(context, response1.body().string(), Toast.LENGTH_SHORT).show();
}
return null;
}
}
Please try the below way
(1) HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
try {
httpClient.addInterceptor(new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
// try the request
Response response = chain.proceed(request);
if (response shows expired token) {
// get a new token (I use a synchronous Retrofit call)
// create a new request and modify it accordingly using the new token
Request newRequest = request.newBuilder().build();
// retry the request
return chain.proceed(newRequest);
}
// otherwise just pass the original response on
return response;
}
});
} catch (Exception e) {
e.printStackTrace();
}
String baseUrl = "";
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiService = retrofit.create(ApiService.class);
(2) public class TokenAuthenticator implements Authenticator {
private final ApiService apiservice;
public TokenAuthenticator(TokenServiceHolder tokenServiceHolder) {
this.tokenServiceHolder = tokenServiceHolder;
}
#Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
newtoken = apiservice.refreshToken().execute();
return response.request().newBuilder()
.header(AUTHORIZATIONKEY, newtoken)
.build();
}
#Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
// Null indicates no attempt to authenticate.
return null;
}
}
Hope it may help you
I'm trying to upload a photo to the server.
My problem is that I can not get a link to this picture, which the server returns to me.
public class RetrofitClient {
public static Retrofit createService(
String username, String password) {
String authToken = Credentials.basic(username, password);
return createService(authToken);
}
public static Retrofit createService(final String authToken) {
AuthenticationInterceptor interceptor = null;
if (!TextUtils.isEmpty(authToken))
interceptor = new AuthenticationInterceptor(authToken);
Gson gson = new GsonBuilder()
.setLenient()
.create();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
final OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.addInterceptor(logging)
.retryOnConnectionFailure(true)
.readTimeout(30, TimeUnit.SECONDS)
.connectTimeout(30, TimeUnit.SECONDS).build();
return new Retrofit.Builder()
.baseUrl(AppConstant.BASE_URL+"/")
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
public static RetroInterface getApiServices(String user_name, String passowrd) {
return createService(user_name, passowrd).create(RetroInterface.class);
}
}
RetroInterface.java
....
#Multipart
#POST("parents/profile/images")
Call<String>
uploadParentProfilePicture2(#Part MultipartBody.Part filePart);
....
Profile.java
....
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
RetroInterface retroInterface = RetrofitClient.getApiServices(getEmail(), getPassword());
Call<String> call = retroInterface.uploadParentProfilePicture(filePart);
call.enqueue(new Callback<String>() {
#Override
public void onResponse(#NonNull Call<String> call, #NonNull Response<String> response) {
Log.d("ldsfjjlk", "response: " + response.body());
}
#Override
public void onFailure(#NonNull Call<String> call, #NonNull Throwable t) {
Log.d("ldsfjjlk", "Throwable: " + t.getLocalizedMessage());
}
});
....
HttpLoggingInterceptor response :
"parents/654/profile/images/1530979485020.jpg"
It is right!
My log:
"parents"
What is this?
Something wrong with Retrofit?
Also I tried to replace
Call<String> to Call<ResponseBody> but:
response.body.string() == null
What did I miss?
I'm trying to send a Json object to a server with android studio, using okhttp3, and my app always crashes when I just try to send the json, when the app says the message was sent. In addition, I need to see in response my own json I created as a confirmation that my Json worked.
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure(okhttp3.Call call, IOException e) {
Log.e("TAG", "Failed sending message!");
Toast.makeText(MainActivity.this,"Failed sending message",Toast.LENGTH_LONG).show();
}
#Override
public void onResponse(okhttp3.Call call, Response response) throws IOException {
Log.d("TAG", "Message sent successfully!");
Log.d("TAG", response.body().string());
Toast.makeText(MainActivity.this,"Message sent successfully!",Toast.LENGTH_LONG).show();
}
});
}
My problem seems to appear in the onResponse and onFaliure functions. Here is the error I get on the variables I put in these functions: http://prntscr.com/i0dhgi
The error appears on all 4 variables, two in onFaliure and two in onResponse
I ran your code on my machine what you need to do is something like that but make sure you have this in you app's build.gradle
compile 'com.android.support:support-annotations:20.0.0' if you are using old android studio version. new versions make project with builtin annotation processor
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure( #NonNull okhttp3.Call call,#NonNull IOException e) {
Log.e("TAG", "Failed sending message!");
//using a toast means updating the UI thread from back thread you have to call Content.runOnUiThread(new Runnable) to sync with the UI thread.
//Toast.makeText(MainActivity.this,"Failed sending message",Toast.LENGTH_LONG).show();
}
#Override
public void onResponse(#NonNull okhttp3.Call call,#NonNull Response response) throws IOException {
Log.d("TAG", "Message sent successfully!");
Log.d("TAG", response.body().string());
//Toast.makeText(MainActivity.this,"Message sent successfully!",Toast.LENGTH_LONG).show();
}
});
}
take a look at the picture I ran the code with dummy values and got to see the logcat clearly saying about thread handling issue!
here is the final solution for you that I made will do the trick
NOTE! you can replace "MainActivity.this" with your local Context
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
void post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
okhttp3.Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure( #NonNull okhttp3.Call call,#NonNull IOException e) {
MyActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
//Handle UI here
// Toast anything you like here//
}
});
}
#Override
public void onResponse(#NonNull okhttp3.Call call,#NonNull Response response) throws IOException {
MyActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
//Handle UI here
//happy on Response Toast here
}
});
}
}
});
}