2017-02-25 35 views
0

發佈的數據我是新使用改造2
我可以得到的數據,但我不能用這種方式發佈的數據
我得到的數據如何利用改造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

比方說你的API來發送一個名爲send_data_here.php數據和它得到json和它的方法是POST所以我們必須有我們的請求對象:(使用this link創建)

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

} 

,你必須有你的方法:

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

現在,如果你打電話給你的API將發送您創建和響應將返回從服務器附帶的回報的事情的數據。

+0

範例,Dd和Ff對您的答案是否重要? –

+0

@mohamed zarei 必填參數缺少:數據 –

+0

@代碼學徒不,我只是用他的JSON創建了一個模型。 –