gpt4 book ai didi

android - 如何在 Android 中使用 Retrofit 发送 JSON POST 请求,并接收字符串响应

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:55 25 4
gpt4 key购买 nike

我是第一次使用 Retrofit2,遇到了一些问题。

这是用于调用 REST API 的代码 fragment

    //building retrofit object
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.71:9000/api/uniapp/")
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();

APIService service = retrofit.create(APIService.class);

//defining the call
Call<String> call = service.refreshAppMetaConfig("0");
//calling the api
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
//displaying the message from the response as toast
System.out.println("Uniapp :"+response);
}

@Override
public void onFailure(Call<String> call, Throwable t) {
System.out.println("Uniapp :"+t.getMessage());
}
});

这是 APIService 类:

public interface APIService {

//The register call
@FormUrlEncoded
@POST("appmetaconfigjson")
Call<String> refreshAppMetaConfig(@Field("versionId") String versionId);
}

我正在使用 Play 框架来创建 REST API。我收到内部服务器错误。 API 无法读取 JSON 请求。但是如果我通过 Postman 访问 API,它会返回响应。有什么建议吗?

我添加了 postman 请求截图。

Postman Sample

最佳答案

正如我从您的 Postman 屏幕截图中看到的那样,您正在将 JSON 正文发送到 REST API。当您选择正文类型为 raw 时- application/json在 Postman 中,它会自动包含

Content-Type:application/json

作为标题。至此,在Postman中请求成功。

现在,为了使其在您的 Android 应用程序中成功运行于请求之上,您需要使用发送到 REST API 的请求设置 header 。

APIService界面进行以下更改。

import retrofit2.http.Body;
import okhttp3.ResponseBody;
import java.util.Map;


public interface APIService {

//The register call
// @FormUrlEncoded <==== comment or remove this line

@Headers({
"Content-Type:application/json"
})
@POST("appmetaconfigjson")
Call<ResponseBody> refreshAppMetaConfig(@Body Map<String, String> versionId);
}
  1. 删除或评论 @FormUrlEncoded注释,因为我们发送的是 JSON 而不是 FormUrlEncoded 数据。
  2. 添加@Headers()带有 Content-Type:application/json 的注释
  3. 将方法参数更改为 @Body Map<String, String> versionId . @Body注释转换(序列化)Map当您向 API 请求时,将 (HashMap) 数据转换为 JSON 正文。
  4. String 更改返回参数至 ResponseBody .

使用上面修改的方法如下

// code...

//defining the call

// create parameter with HashMap
Map<String, String> params = new HashMap<>();
params.put("versionId", "0");

Call<ResponseBody> call = service.refreshAppMetaConfig(params);
//calling the api
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//displaying the message from the response as toast

// convert ResponseBody data to String
String data = response.body().string();

System.out.println("Uniapp : " + data);
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
System.out.println("Uniapp : " + t.getMessage());
}
});

这里您还需要更改 Call<String> 中的参数至 Call<ResponseBody> .并在 onResponse() 内转换响应使用 response.body().string(); 的方法.

关于android - 如何在 Android 中使用 Retrofit 发送 JSON POST 请求,并接收字符串响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52110913/

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