欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android如何通过Retrofit提交Json格式数据

程序员文章站 2023-12-04 09:50:52
本文将介绍如何通过retrofit库post一串json格式的数据。首先post的json数据格式如下: { "id": "string", "dev...

本文将介绍如何通过retrofit库post一串json格式的数据。首先post的json数据格式如下:

{
 "id": "string",
 "deviceid": "string",
 "name": "string",
 "sumdistance": 0,
 "routeno": "string",
 "sumpoints": 0,
 "setuptime": "2016-06-10t13:11:00.766z",
 "usedtime": 0,
 "points": [
  {
   "id": "string",
   "routeno": "string",
   "name": "string",
   "longitude": "string",
   "latitude": "string",
   "height": 0,
   "distance": 0,
   "yaw": 0,
   "pitch": 0,
   "speed": 0,
   "usedtime": 0
  }
 ]
}

通过安装android studio gsonformat插件,根据上面的json格式自动生成一个bean类,本文命名为flyroutebean,

package com.example.administrator.retrofitex; 
import java.util.list; 
 
import android.os.parcel; 
import android.os.parcelable; 
 
/** 
 * created by administrator on 2016/6/10. 
 */ 
public class flyroutebean{ 
 
  /** 
   * id : string 
   * deviceid : string 
   * name : string 
   * sumdistance : 0 
   * routeno : string 
   * sumpoints : 0 
   * setuptime : 2016-05-23t06:20:50.254z 
   * usedtime : 0 
   * points : [{"id":"string","routeno":"string","name":"string","longitude":"string","latitude":"string","height":0,"distance":0,"yaw":0,"pitch":0,"speed":0,"usedtime":0}] 
   */ 
 
  public string id; 
  public string deviceid; 
  public string name; 
  public double sumdistance; 
  public string routeno; 
  public int sumpoints; 
  public string setuptime; 
  public double usedtime; 
  /** 
   * id : string 
   * routeno : string 
   * name : string 
   * longitude : string 
   * latitude : string 
   * height : 0 
   * distance : 0 
   * yaw : 0 
   * pitch : 0 
   * speed : 0 
   * usedtime : 0 
   */ 
 
  public list<pointsbean> points; 
 
  public string getid() { 
    return id; 
  } 
 
  public void setid(string id) { 
    this.id = id; 
  } 
 
  public string getdeviceid() { 
    return deviceid; 
  } 
 
  public void setdeviceid(string deviceid) { 
    this.deviceid = deviceid; 
  } 
 
  public string getname() { 
    return name; 
  } 
 
  public void setname(string name) { 
    this.name = name; 
  } 
 
  public double getsumdistance() { 
    return sumdistance; 
  } 
 
  public void setsumdistance(double sumdistance) { 
    this.sumdistance = sumdistance; 
  } 
 
  public string getrouteno() { 
    return routeno; 
  } 
 
  public void setrouteno(string routeno) { 
    this.routeno = routeno; 
  } 
 
  public int getsumpoints() { 
    return sumpoints; 
  } 
 
  public void setsumpoints(int sumpoints) { 
    this.sumpoints = sumpoints; 
  } 
 
  public string getsetuptime() { 
    return setuptime; 
  } 
 
  public void setsetuptime(string setuptime) { 
    this.setuptime = setuptime; 
  } 
 
  public double getusedtime() { 
    return usedtime; 
  } 
 
  public void setusedtime(double usedtime) { 
    this.usedtime = usedtime; 
  } 
 
  public list<pointsbean> getpoints() { 
    return points; 
  } 
 
  public void setpoints(list<pointsbean> points) { 
    this.points = points; 
  } 
 
  public static class pointsbean implements parcelable { 
    public string id; 
    public string routeno; 
    public string name; 
    public string longitude; 
    public string latitude; 
    public double height; 
    public double distance; 
    public double yaw; 
    public double pitch; 
    public double speed; 
    public double usedtime; 
 
    public string getid() { 
      return id; 
    } 
 
    public void setid(string id) { 
      this.id = id; 
    } 
 
    public string getrouteno() { 
      return routeno; 
    } 
 
    public void setrouteno(string routeno) { 
      this.routeno = routeno; 
    } 
 
    public string getname() { 
      return name; 
    } 
 
    public void setname(string name) { 
      this.name = name; 
    } 
 
    public string getlongitude() { 
      return longitude; 
    } 
 
    public void setlongitude(string longitude) { 
      this.longitude = longitude; 
    } 
 
    public string getlatitude() { 
      return latitude; 
    } 
 
    public void setlatitude(string latitude) { 
      this.latitude = latitude; 
    } 
 
    public double getheight() { 
      return height; 
    } 
 
    public void setheight(double height) { 
      this.height = height; 
    } 
 
    public double getdistance() { 
      return distance; 
    } 
 
    public void setdistance(double distance) { 
      this.distance = distance; 
    } 
 
    public double getyaw() { 
      return yaw; 
    } 
 
    public void setyaw(double yaw) { 
      this.yaw = yaw; 
    } 
 
    public double getpitch() { 
      return pitch; 
    } 
 
    public void setpitch(double pitch) { 
      this.pitch = pitch; 
    } 
 
    public double getspeed() { 
      return speed; 
    } 
 
    public void setspeed(double speed) { 
      this.speed = speed; 
    } 
 
    public double getusedtime() { 
      return usedtime; 
    } 
 
    public void setusedtime(double usedtime) { 
      this.usedtime = usedtime; 
    } 
 
    @override 
    public string tostring() { 
      return "pointsbean{" + 
          "id='" + id + '\'' + 
          ", routeno='" + routeno + '\'' + 
          ", name='" + name + '\'' + 
          ", longitude='" + longitude + '\'' + 
          ", latitude='" + latitude + '\'' + 
          ", height=" + height + 
          ", distance=" + distance + 
          ", yaw=" + yaw + 
          ", pitch=" + pitch + 
          ", speed=" + speed + 
          ", usedtime=" + usedtime + 
          '}'; 
    } 
 
    @override 
    public void writetoparcel(parcel dest, int flags) { 
      // todo auto-generated method stub 
      dest.writestring(id); 
      dest.writestring(routeno); 
      dest.writestring(name); 
      dest.writestring(longitude); 
      dest.writestring(latitude); 
      dest.writedouble(height);     
      dest.writedouble(distance); 
      dest.writedouble(yaw); 
      dest.writedouble(pitch); 
      dest.writedouble(speed); 
      dest.writedouble(usedtime);    
    } 
    public static final creator<pointsbean> creator=new creator<pointsbean>() { 
 
      @override 
      public pointsbean createfromparcel(parcel source) { 
        // todo auto-generated method stub 
        pointsbean pointsbean=new pointsbean(); 
        pointsbean.setid(source.readstring()); 
        pointsbean.setrouteno(source.readstring()); 
        pointsbean.setname(source.readstring()); 
        pointsbean.setlongitude(source.readstring()); 
        pointsbean.setlatitude(source.readstring()); 
        pointsbean.setheight(source.readint()); 
        pointsbean.setdistance(source.readint()); 
        pointsbean.setyaw(source.readint()); 
        pointsbean.setpitch(source.readint()); 
        pointsbean.setspeed(source.readint()); 
        pointsbean.setusedtime(source.readint()); 
        return pointsbean; 
      } 
 
      @override 
      public pointsbean[] newarray(int size) { 
        // todo auto-generated method stub 
        return new pointsbean[size]; 
      } 
       
    }; 
 
    @override 
    public int describecontents() { 
      // todo auto-generated method stub 
      return 0; 
    } 
  } 
 
  @override 
  public string tostring() { 
    return "flyroutebean{" + 
        "id='" + id + '\'' + 
        ", deviceid='" + deviceid + '\'' + 
        ", name='" + name + '\'' + 
        ", sumdistance=" + sumdistance + 
        ", routeno='" + routeno + '\'' + 
        ", sumpoints=" + sumpoints + 
        ", setuptime='" + setuptime + '\'' + 
        ", usedtime=" + usedtime + 
        ", points=" + points + 
        '}'; 
  } 
 
   
} 

然后就来建立接口了,其内容如下:

public interface postroute { 
  @headers({"content-type: application/json","accept: application/json"})//需要添加头 
  @post("api/flyroute/add") 
  call<flyroutebean> postflyroute(@body requestbody route);//传入的参数为requestbody 
} 

接下来就是提交数据的了:

flyroutebean flyroutebean=new flyroutebean(); 
flyroutebean=initdata(flyroutebean);//根据bean类初始化一个需要提交的数据类 
gson gson=new gson(); 
string route= gson.tojson(flyroutebean);//通过gson将bean转化为json字符串形式 
 retrofit retrofit=new retrofit.builder() 
      .baseurl(url) 
      .addconverterfactory( gsonconverterfactory.create()) 
      .build(); 
  postroute postroute=retrofit.create(postroute.class); 
  requestbody body=requestbody.create(okhttp3.mediatype.parse("application/json; charset=utf-8"),route); 
  call<flyroutebean> call=postroute.postflyroute(body); 
  call.enqueue(new callback<flyroutebean>() { 
  @override 
  public void onresponse(call<flyroutebean> call, response<flyroutebean> response) { 
    log.e("sssss","-----------------------"+response.body().getdeviceid());//这里是用于测试,服务器返回的数据就是提交的数据。 
  } 
 
  @override 
  public void onfailure(call<flyroutebean> call, throwable t) { 
    log.e("sssss",t.getmessage()); 
  } 
}); 

<----------- 更新如何同时提交json数据和其他查询字段:--------------->

服务器端(asp):核心代码如下,接收一个json格式学生类,加一个bool类型

 [httppost]
  public ihttpactionresult info(student stu, bool isgay)
    {
      
      return ok(stu.name);
    }

客户端:

//接口地址:http://172.20.69.209:9665/api/test/info?isgay={isgay}
@post("api/test/info")
call<responsebody> poststudent(@body requestbody stu,@query("isgay") boolean isgay);

实现代码:

student stu=new student();
stu.setid("20103177");
stu.setname("zpm");
stu.setage(18);
gson gson=new gson();
string route= gson.tojson(stu);
log.e("post", "//");

retrofit retrofit=new retrofit.builder()
    .baseurl("http://172.20.69.209:9665/")
    .addconverterfactory( gsonconverterfactory.create())
    .build();
postroute postroute=retrofit.create(postroute.class);
requestbody body=requestbody.create(okhttp3.mediatype.parse("application/json; charset=utf-8"),route);
call<responsebody> call=postroute.poststudent(body,true);
call.enqueue(new callback<responsebody>() {
  @override
  public void onresponse(call<responsebody> call, response<responsebody> response) {
    try {
      log.i("onresponse", response.body().string());
    } catch (ioexception e) {
      e.printstacktrace();
    }
  }

  @override
  public void onfailure(call<responsebody> call, throwable t) {
    log.e("onfailure", t.getmessage());
  }
});

log.e("postjson", route);

结果:

Android如何通过Retrofit提交Json格式数据

具体代码已经传到github

ps:本文中服务器都是楼主在局域网搭建的,

需要添加的依赖:

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'

源代码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。