简体   繁体   English

如何使用Android的Retrofit 2发布数据

[英]How to post data using retrofit 2 from android

I am new in using retrofit 2 我是使用改造2的新手
I can get data but i can't post data 我可以获取数据,但不能发布数据
I get data by this way 我通过这种方式获取数据

My ApiClient class is 我的ApiClient类是

public class ApiClient {

    public static final String BASE_URL = "http://test.com/test/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

My ApiInterface interface is 我的ApiInterface界面是

public interface ApiInterface {
    @GET("test1.php")
    Call<data1> getMethod1();

    @GET("test2.php")
    Call<data2> getMethod2();

}

In my MainActivity.class is 在我的MainActivity.class

 ApiInterface apiService =
      ApiClient.getClient().create(ApiInterface.class);

      Call<data1> call = apiService.getdata1();
  call.enqueue(new Callback<data1>() {
    @Override
    public void onResponse(Call<data1> call, Response<data1> response) {
      data1List = response.body().getdata1data();
      GoToMain();

    }

    @Override
    public void onFailure(Call<data1> call, Throwable t) {


  });

how can i post this data 我如何发布此数据

URL         : http://http://test.com/test/FeedBack.php
Request Sample : POST : /FeedBack.php
Request parameter name : data
Request parameter value : 
{  
   "dd":{  
      "nn":"qwexcvsd",
      "mm":1,
      "gg":1
   },
   "ss":{  
      "ss_id":1
   },
   "contactcard":{  
      "address":"asd",
      "phone1":"123123",
      "phone2":"",
      "phone3":"",
      "phone4":"",
      "email":"",
      "comment":"",
      "gg_id":1,
      "ii":"1"
   },
   "ff":{  
      "dd":"asdas",
      "user_id":11,
      "AccessToken":"eb5e72cbeb94bda0e5c43ab8f4a23af6",
      "wwee:2,
      "asd":"2014-07-18 04:46:01"
   }
}

please any help because i searched a lot and didn't reach anything 请任何帮助,因为我搜索了很多东西却没有找到任何东西

Lets say your api to send data named send_data_here.php and it gets json and its method is POST so we must have our request object: (created using this link ) 假设您的api发送名为send_data_here.php数据,它获取json ,其方法为POST因此我们必须具有我们的request对象:(使用此链接创建)

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Contactcard {

@SerializedName("address")
@Expose
private String address;
@SerializedName("phone1")
@Expose
private String phone1;
@SerializedName("phone2")
@Expose
private String phone2;
@SerializedName("phone3")
@Expose
private String phone3;
@SerializedName("phone4")
@Expose
private String phone4;
@SerializedName("email")
@Expose
private String email;
@SerializedName("comment")
@Expose
private String comment;
@SerializedName("gg_id")
@Expose
private Integer ggId;
@SerializedName("ii")
@Expose
private String ii;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPhone1() {
return phone1;
}

public void setPhone1(String phone1) {
this.phone1 = phone1;
}

public String getPhone2() {
return phone2;
}

public void setPhone2(String phone2) {
this.phone2 = phone2;
}

public String getPhone3() {
return phone3;
}

public void setPhone3(String phone3) {
this.phone3 = phone3;
}

public String getPhone4() {
return phone4;
}

public void setPhone4(String phone4) {
this.phone4 = phone4;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

public Integer getGgId() {
return ggId;
}

public void setGgId(Integer ggId) {
this.ggId = ggId;
}

public String getIi() {
return ii;
}

public void setIi(String ii) {
this.ii = ii;
}

}
-----------------------------------com.example.Dd.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Dd {

@SerializedName("nn")
@Expose
private String nn;
@SerializedName("mm")
@Expose
private Integer mm;
@SerializedName("gg")
@Expose
private Integer gg;

public String getNn() {
return nn;
}

public void setNn(String nn) {
this.nn = nn;
}

public Integer getMm() {
return mm;
}

public void setMm(Integer mm) {
this.mm = mm;
}

public Integer getGg() {
return gg;
}

public void setGg(Integer gg) {
this.gg = gg;
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("dd")
@Expose
private Dd dd;
@SerializedName("ss")
@Expose
private Ss ss;
@SerializedName("contactcard")
@Expose
private Contactcard contactcard;
@SerializedName("ff")
@Expose
private Ff ff;

public Dd getDd() {
return dd;
}

public void setDd(Dd dd) {
this.dd = dd;
}

public Ss getSs() {
return ss;
}

public void setSs(Ss ss) {
this.ss = ss;
}

public Contactcard getContactcard() {
return contactcard;
}

public void setContactcard(Contactcard contactcard) {
this.contactcard = contactcard;
}

public Ff getFf() {
return ff;
}

public void setFf(Ff ff) {
this.ff = ff;
}

}
-----------------------------------com.example.Ff.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Ff {

@SerializedName("dd")
@Expose
private String dd;
@SerializedName("user_id")
@Expose
private Integer userId;
@SerializedName("AccessToken")
@Expose
private String accessToken;
@SerializedName("wwee")
@Expose
private Integer wwee;
@SerializedName("asd")
@Expose
private String asd;

public String getDd() {
return dd;
}

public void setDd(String dd) {
this.dd = dd;
}

public Integer getUserId() {
return userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

public String getAccessToken() {
return accessToken;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

public Integer getWwee() {
return wwee;
}

public void setWwee(Integer wwee) {
this.wwee = wwee;
}

public String getAsd() {
return asd;
}

public void setAsd(String asd) {
this.asd = asd;
}

}
-----------------------------------com.example.Ss.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Ss {

@SerializedName("ss_id")
@Expose
private Integer ssId;

public Integer getSsId() {
return ssId;
}

public void setSsId(Integer ssId) {
this.ssId = ssId;
}

}

and you must have your method: 并且您必须拥有自己的方法:

@POST("send_data_here.php")
    Call<TheResponseOfTheApi> sendData(@Body Contactcard card);

Now if you call your api it will send the data that you created and in response it will return the return thing that came from server. 现在,如果您调用api,它将发送您创建的数据,并且作为响应,它将返回来自服务器的返回信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM