实际上,我正在尝试使用Retrofit将txt文件发送到服务器。 但是当我实际尝试发送它时,它从onFailure方法中给我以下错误
我是android的新手,通过查看一些改装指南,我仍然无法理解我做错了什么,即使我以正确的方式使用改装,如果有人能够帮帮我。
E / TAG:无法将帖子提交到API。
但是实际上它是在服务器上创建一个文件夹但没有发送文件,我在做什么错了?
这是我的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);
}
这里是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;
}
}
这是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);
}
这里是sendPost()方法,我用来通过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());
}
});
}
如果有人会建议我如何通过http请求改进改版的性能,我将不胜感激。
答案 0 :(得分:1)
使用Void代替空类
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);
答案 1 :(得分:0)
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) {
}
});
}