Retrofit提交Json

本文详细介绍了如何使用Retrofit框架创建APIService接口类,并通过实例演示了如何调用自定义的POST方法。文章深入解析了@Headers、@POST注解的使用,以及如何处理响应数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、APIService为自定义接口类,

   定义@Headers

@Headers({"Content-Type: application/json","Accept: application/json"})
@POST
Call<ResponseBody> Test(@Url String url,@Body RequestBody route);//测试使用@Url

2、调用代码如下,使用了Call<ResponseBody>,

 


 APIService req;
 req = RetrofitManager.getInstance().createReq(APIService.class);
 HashMap hashMap = new HashMap();
 Retrofit retrofit=new Retrofit.Builder()
     .baseUrl(AppConfig.BASE_URL)
     .build();
 APIService postRoute=retrofit.create(APIService.class);
 RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),datas);//datas是json字符串
 Call<ResponseBody> call=postRoute.Test("/test.shtml?type=test",body);
 call.enqueue(new Callback<ResponseBody>() {
 @Override
 public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
 try {
      String json = response.body().string();
      JSONObject jObject1 = new JSONObject(json);
      String result = jObject1.getString("result");
      String message = jObject1.getString("message");
      if (!result.equals("0"))//不为0异常
      {
           showToast("异常:"+message);
      }
      else
      {
           showToast("成功:"+message);
      }
     }
     catch (Exception ex)
     {

     }
          

 

转载于:https://www.cnblogs.com/zhaogaojian/p/9748910.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值