Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法向API改装提交post_Java_Android_Http_Retrofit - Fatal编程技术网

Java 无法向API改装提交post

Java 无法向API改装提交post,java,android,http,retrofit,Java,Android,Http,Retrofit,实际上,我正在尝试使用改型向服务器发送一个txt文件。 但当我实际尝试发送它时,它从onFailure方法中给我以下错误 我是android新手,通过查看一些关于改型的教程,我仍然不知道我做错了什么,即使我使用的是正确的改型方式,如果有人能够帮助我,那将是非常棒的 E/TAG:无法向API提交帖子 但实际上它在服务器上创建了一个文件夹,但没有发送文件,我做错了什么 这是我的ApiUtils.java import okhttp3.RequestBody; import retrofit2.Cal

实际上,我正在尝试使用改型向服务器发送一个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);
}
下面是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);
}
导入okhttp3.MultipartBody;
2.电话;;
导入文件2.http.Multipart;
导入文件2.http.POST;
导入2.http.Part;
公共接口服务{
@多部分
@POST(“UPD.aspx?CART=PTERM”)
调用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());
        }
    });
}
public void sendPost(){
File File=新文件(“/data/data/com.example.igardini.visualposmobile/files/scontrino.txt”);
MultipartBody.Builder=新的MultipartBody.Builder();
setType(MultipartBody.FORM);
builder.addFormDataPart(“scontrino”,file.getName());
MultipartBody requestBody=builder.build();
APIService=ApiUtils.getAPIService();
Call Call=apiService.savePost(requestBody);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
}否则{
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“TAG”,t.toString());
}
});
}
如果有人能建议我如何通过http请求改进改进性能,我将不胜感激。

private void sendPost(){
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) {

        }
    });
}
MultipartBody.Builder=新的MultipartBody.Builder(); setType(MultipartBody.FORM); builder.addFormDataPart(“scontrino”,file.getName()); MultipartBody requestBody=builder.build(); MainInterface apiService=ApiClient.getClient().create(MainInterface.class); Call Call=apiService.savePost(requestBody); call.enqueue(新回调(){ @凌驾 公共void onResponse(调用、响应){ progressBarHandler.disclose(); if(response.issusccessful()){ }否则{ } } @凌驾 失败时公共无效(调用调用,可丢弃的t){ } }); }
使用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.");
    }
});
}

在APIService.java类中,用响应替换postclass@Maitri哦,对了,我甚至还有Post.java空的。如何创建响应类?更改该空响应的名称class@Maitri添加到问题Post.java中,您对response类是什么意思?实际上,我的服务器在上传数据时没有任何响应,我该怎么做呢?现在只需创建一个空白的java类,比如MyResponse.javaOk我没有收到任何错误,但文件仍然没有发送,它正在服务器上创建文件夹,但在itcheck if
response.issusccessful()中没有文件
是真的这是我得到的响应响应{protocol=http/1.1,code=200,message=OK,url=}我想你没有传递文件试试这个,
RequestBody fbody=RequestBody.create(MediaType.parse(“text/*”),file);builder.addFormDataPart(“scontrino”,file.getName(),fbody)替换
builder.addFormDataPart(“scontrino”,file.getName())用那个
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.");
    }
});
}
RequestBody fbody = RequestBody.create(MediaType.parse("text/*"), file); 
builder.addFormDataPart("scontrino", file.getName(),fbody);