Reputation: 4307
Actually i'm trying to send a txt file to a server using Retrofit. But when i actually trying to send it it gives me the following error from onFailure method
I'm new in android and by looking on some tutorials on retrofit i can't still get what am i doing wrong and even if i'm using in right way retrofit, that would be great if someone will be able to help me.
E/TAG: Unable to submit post to API.
But actually it's creating a folder on the server but not sending the file, what i'm doing wrong?
Here is my ApiUtils.java
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface APIService {
@POST("UPD.aspx?CART=PTERM")
Call<MyResponse> savePost(@Body RequestBody text);
}
Here is RetrofitClient.java
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Here is APIService.java
import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
public interface APIService {
@Multipart
@POST("UPD.aspx?CART=PTERM")
Call<Void> savePost(@Part MultipartBody.Part text);
}
Here is sendPost() method which i use to send the file via onClick
public void sendPost() {
File file = new File("/data/data/com.example.igardini.visualposmobile/files/scontrino.txt");
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("scontrino", file.getName());
MultipartBody requestBody = builder.build();
APIService apiService = ApiUtils.getAPIService();
Call<Void> call = apiService.savePost(requestBody);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
} else {
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Log.e("TAG", t.toString());
}
});
}
And i'll appreciate if even someone will suggest me on how can i improve performance of retrofit with it's http requests.
Upvotes: 1
Views: 566
Reputation: 543
private void sendPost() {
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("scontrino", file.getName());
MultipartBody requestBody = builder.build();
MainInterface apiService = ApiClient.getClient().create(MainInterface.class);
Call<MyResponse> call = apiService.savePost(requestBody);
call.enqueue(new Callback<MyResponse>() {
@Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
progressBarHandler.dismiss();
if (response.isSuccessful()) {
} else {
}
}
@Override
public void onFailure(Call<MyResponse> call, Throwable t) {
}
});
}
Upvotes: 1
Reputation: 5017
Use Void instead of empty class
public void sendPost(MultipartBody.Part txt) {
mAPIService.savePost(txt).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if(response.isSuccessful()) {
Log.i("TAG", "post submitted to API." + response.body().toString());
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Log.e("TAG", "Unable to submit post to API.");
}
});
}
Request body
RequestBody fbody = RequestBody.create(MediaType.parse("text/*"), file);
builder.addFormDataPart("scontrino", file.getName(),fbody);
Upvotes: 1