如何使用android中的改造2发布数据

时间:2017-02-25 15:20:09

标签: android retrofit2

我是使用改造2的新手 我可以获取数据,但我无法发布数据 我通过这种方式得到数据

我的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;
    }
}

我的ApiInterface界面

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

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

}

我的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) {


  });

我如何发布此数据

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"
   }
}

请任何帮助,因为我搜索了很多但没有达到任何帮助

1 个答案:

答案 0 :(得分:1)

让我们说你的api发送名为document.getElementsById("occ").innerHTML = "Already registered";的数据,它得到send_data_here.php,其方法是json所以我们必须有我们的请求对象:(使用this link创建)

POST

你必须拥有自己的方法:

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;
}

}

现在,如果您调用api,它将发送您创建的数据,作为回应,它将返回来自服务器的返回内容。