1 首先你要定义一个接口

  @POST
Call<String> post(@Url String url, @Body String info);

2 创建一个service

        public static RetrofitHttpService createRetrofitHttpService() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL + "/")
.addConverterFactory(StringConverterFactory.create())
.build(); RetrofitHttpService Service =
retrofit.create(RetrofitHttpService.class);
return Service;
}

3 需要添加一个string转换器

package org.droidplanner.lelefly.retrofit;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type; import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.Buffer;
import retrofit2.Converter;
import retrofit2.Retrofit; public class StringConverterFactory extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
public static StringConverterFactory create() {
return new StringConverterFactory();
} private StringConverterFactory() { } @Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return new Converter<ResponseBody, String>(){
@Override
public String convert(ResponseBody value) throws IOException {
return value.toString();
}
};
} @Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return new Converter<String, RequestBody>(){
@Override
public RequestBody convert(String value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(), "utf-8");
writer.write(value);
writer.close();
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
};
}
}

4 就可以传递jsonarray了,我下面的实例代码传递的是jsonarray

        HttpUtil.post(Lconstant.base + Lconstant.ADD_UAV_MISSION,"[" +
resultStr +"]", new ResultCallBack() {
@Override
public void onSuccess(String url, String model) {
Log.i("===>>",model);
showToast("上传服务器成功"); } @Override
public void onFailure(int statusCode, String errorMsg) {
showToast(errorMsg);
}
});

Retrofit 传递json 和 复杂参数类型List<T>的更多相关文章

  1. Ajax中传递Json格式的参数

    $.ajax({ type: "post", url: baseUrl+"sys/login", dataType: "json", con ...

  2. Web api Json 接受的参数类型为父类,自动序列化为子类的过程

    场景: public abstract class JsonCreationConverter<T> : JsonConverter { /// <summary> /// t ...

  3. JMeter传递JSON数据

    步骤: 1.添加线程组.HTTP请求默认值.察看结果树等参考<JMeter实现bugfree登录接口测试>.这里不再赘述. 2.添加HTTP请求 在Body Data中写上输入的参数.参数 ...

  4. WebApi传递JSON参数

    开发过程中经常进行JSON的传递,在WebApi中传递JSON字串时,会发现服务器端接收到不参数值,看下面代码 服务端: public void Post([FromBody]string value ...

  5. Spring MVC(七)--传递JSON参数

    有时候参数的传递还需要更多的参数,比如一个获取用户信息的请求中既有用户ID等基本参数,还要求对查询结果进行分页,针对这种场景,一般都会将分页参数封装成一个对象,然后将它和基本参数一起传给控制器,为了控 ...

  6. C# WebService的简单和复杂参数类型和结果的JSON格式

    Jquery作为一款优秀的JS框架,简单易用的特性就不必说了.在实际的开发过程中,使用JQ的AJAX函数调用WebService 的接口实现AJAX的功能也成了一种比较普遍的技术手段了.WebServ ...

  7. jQuery调用WCF服务传递JSON对象

    下面这个示例使用了WCF去创建一个服务端口从而能够被ASP.Net页面通过jQuery的AJAX方法访问,我们将在客户端使用Ajax技术来 与WCF服务进行通信.这里我们仅使用jQuery去连接Web ...

  8. Jquery调用Webservice传递Json数组

    Jquery由于提供的$.ajax强大方法,使得其调用webservice实现异步变得简单起来,可以在页面上传递Json字符串到Webservice中,Webservice方法进行业务处理后,返回Js ...

  9. SpringMVC传递JSON数据

    文章目录 一.前后端传递和接收JSON数据 1:是要Ajax默认格式来传递数据(*) 2:使用application/json格式来传递数据 二.spring-web.xml中需要如下配置 一.前后端 ...

随机推荐

  1. (C/C++学习)5.C++中的虚继承-虚函数-多态解析

    说明:在C++学习的过程中,虚继承-虚函数经常是初学者容易产生误解的两个概念,它们与C++中多态形成的关系,也是很多初学者经常产生困惑的地方,这篇文章将依次分别对三者进行解析,并讲述其之间的联系与不同 ...

  2. [LUOGU]4932 浏览器

    \(\_\_stdcall\)大佬出的题\(Orz\) 我们惊奇地发现,加入\(\_\_popcount(x)\)和\(\_\_popcount(y)\)的奇偶数性相同,那么\(\_\_popcoun ...

  3. 51.percentiles rank以及网站访问时延SLA统计

    主要知识点: percentile_ranks的用法 percentile的优化     一.percentile_ranks的用法 SLA:就是所提供的服务的标准. 比如一个网站的提供的访问延时的S ...

  4. docker的容器可视化工具portainer

    1.搜索镜像 [root@holly ~]# docker search portainer 2.下载portainer [root@holly ~]# docker pull portainer/p ...

  5. (40). springboot + devtools(热部署)【从零开始学Spring Boot】

    我们之前在在()Spring Boot热部署[从零开始学Spring Boot] (http://412887952-qq-com.iteye.com/blog/2291518 )讲过通过使用spri ...

  6. hdu 4081 最小生成树变形

    /*关于最小生成树的等效边,就是讲两个相同的集合连接在一起 先建立一个任意最小生成树,这条边分开的两个子树的节点最大的一个和为A,sum为最小生成树的权值和,B为sum-当前边的权值 不断枚举最小生成 ...

  7. 【ACM】hdu_zs3_1007_Rails_201308100802

    Rails Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)Total Submissi ...

  8. [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

    Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...

  9. cogs 259. 亲戚

    259. 亲戚 ★   输入文件:relations.in   输出文件:relations.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]     或许你并不知道,你 ...

  10. bootstrap-table与Spring项目集成实例收集

    bootstrap-table项目官网:https://github.com/wenzhixin/bootstrap-table bootstrap-table各版本下载:https://github ...