如何在Android中使用Retrofit发送JSON POST请求并接收String响应

阿琼·伊萨(Arjun Issar)

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

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

    //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,它将返回响应。有什么建议么?

香港专业教育学院添加了邮递员的请求截图。

邮递员样本

沙珊斯

从邮递员的屏幕快照中可以看到,您正在将JSON正文发送到REST API。在邮递员中将身体类型选择为raw-application/json时,它会自动包括

Content-Type:application/json

作为标题。因此,该请求在邮递员中成功。

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

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当您请求API时@Body注释会将(MapHashMap)数据转换(序列化)为JSON主体。
  4. 将返回参数从更改StringResponseBody

使用如下修改的方法

// 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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在单元测试中使用JSON发送请求

如何在Django 1.6中使用HTTP POST请求接收json数据?

如何发送POST请求并获得文件响应?

Android / Java:POST中使用RetroFit发送参数并接收JSON响应的问题

如何获得Retrofit POST请求的响应

如何使用HTML发送SOAP请求并接收响应?

Python:如何发送多个HTTP请求并接收响应?

发送ajax请求和接收响应(同步)

使用``Requests''python模块进行POST请求,接收响应就像是GET

如何使用POST方法在Android中使用Retrofit发送原始JSON

使用QT发送POST请求并阅读Json响应

在node.js中,我正在发出一个POST请求,在接收到来自POST请求的响应之前,该函数正在向端点发送空间

使用JSON正文发送POST请求时出现400响应

无法接收来自Post请求的Node.js响应(不使用Express)

如何在Lua中使用OAUTH发送POST请求

如何在Qt中使用JSON正文发送POST请求

使用Retrofit 2 Android将数组的POST请求发送到JSON

如何在使用$ .post来发送和接收数据到PHP以及完成和失败功能时读取JSON响应

如何在android中使用url发送参数并获得响应

使用Sinatra从Instagram接收POST请求

如何在Swift中使用变量发送POST请求

如何在 IB API 中接收响应时发送请求?

如何在 Retrofit 2 (Android) 中发送 POST 请求

如何从 Django Rest Framework 接收 POST 请求?

如何接收使用guzzle发送的post方法的响应

如何在 Ruby 中使用 Post 请求发送 XML 文件

从 Node.js 中的 POST 请求接收 JSON 响应

从 AJAX POST 请求接收 JSON 响应?

如何使用 JavaScript HTML 发送和接收 POST 请求