gpt4 book ai didi

java - Retrofit2 POST 方法获取代码 400,但适用于 ReSTLet 客户端

转载 作者:行者123 更新时间:2023-12-02 00:56:07 25 4
gpt4 key购买 nike

我正在使用Retrofit2。我想从我的移动设备将一些数据发布到我的 Web 服务,但总是收到代码 400 响应。当我尝试从 ReSTLet 客户端发送 POST 请求时,我得到正常响应 (chrome-extension://aejoelaoggembcahagimdiliamlcdmfm/reSTLet_client.html#requests)。

我得到一些信息,问题可能出在服务器证书中,所以我实现了 OkHttpClient,但我仍然得到相同的结果。如果我尝试 POST 到 jsonPlaceHolder 我会得到正常结果 ( https://jsonplaceholder.typicode.com/posts )

API接口(interface):

public interface JsonPlaceHolderApi {

@POST("hws/hibisWsTemplate/api/v1/order/posts")
Call<Post> createPost(@Body Post post);
}

我的帖子类(class):

public class Post {
private String vodilni_slog;
private String IBAN_placnka;
private String polog;
...

public Post(String vodilni_slog, String IBAN_placnka, String polog) {
this.vodilni_slog = vodilni_slog;
this.IBAN_placnka = IBAN_placnka;
this.polog = polog;
...
}

//getters and setters
...

}

主要 Activity (仅发送 POST 请求的部分):

...

send_btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {

//RETROFIT
OkHttpClient okHttpClient = getUnsafeOkHttpClient();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://xxxxxx.x-xx.xx/") //url
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();

jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);


Post post = new Post("test", "test", "test", "test");

Call<Post> call = jsonPlaceHolderApi.createPost(post);

call.enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {

if (!response.isSuccessful()) {
MainActivity.content.setText("Code: " + response.code());
return;
}
Post postResponse = response.body();

String x = "";
x += "Code: " + response.code() + "\n";
x += "Naziv: " + postResponse.getIme_prejemnika() + "\n";
x += "IBAN: " + postResponse.getIBAN_prejemnika() + "\n";
x += "Sklic: " + postResponse.getReferenca_prejemnika() + "\n";

MainActivity.content.setText(x);
}

@Override
public void onFailure(Call<Post> call, Throwable t) {
MainActivity.content.setText(t.getMessage());
}
});
}
});

OKHTTP 客户端:

private static OkHttpClient getUnsafeOkHttpClient() {
try {
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};

final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});

OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

我期望代码 201 带有正文:{ "vodilni_slog": "字符串", “polog”:“字符串”, “dvig”:“字符串”, ...{

但我得到代码 400...

最佳答案

我发现我收到了错误 400,因为 IBAN_placnka 应该是 iban_placnka,所以如果您收到错误 400,请检查您的拼写并使用此行查看您实际收到的 json 格式内容:

Log.e("x" , "Request is" + new Gson().toJson(post).toString() );

关于java - Retrofit2 POST 方法获取代码 400,但适用于 ReSTLet 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com