Android Post Json 使用 Retrofit 2 向服务器发送数据

Android Post Json Data to server using Retrofit 2

你好我在服务器上有简单的 Json 到 post

{
    "Key":"KEY",
    "Token":"",
    "Data":
    {
         "Country": "India",
         "CountryCode":"IN",
         "MobileNumber":"+91111111111"
    }
}

我如何post将此数据发送到服务器请帮帮我 我已经为此创建了接口我已经写了

 @POST("/user/register/")
Call<Login> Registration(@Body Login login, Callback<Login> callBack);

这样可以吗?我怎么称呼它?

我用过

Login login = new Login();
        com.j2ml.casualtrack.pojo_Request.Data data = new com.j2ml.casualtrack.pojo_Request.Data();
        data.setMobileNumber("+911234567890");
        data.setCountry("India");
        data.setCountryCode("IN");
        login.setData(data);
        login.setToken("MY TOKEN KEY");
        login.setKey("");
        final ProgressDialog loading = ProgressDialog.show(this, "Registration in progress..", "Please wait...", false, false);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Urls.URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(RestAPI.class);
        service.Registration(login, new Callback<Login>() {
            @Override
            public void onResponse(Response<Login> response, Retrofit retrofit) {
                System.out.println("Response: " + response.isSuccess());
                loading.dismiss();
            }

            @Override
            public void onFailure(Throwable t) {
                System.out.println("ERROR ..."+t.getMessage());
            }
        });

但是,我遇到了这个错误

 java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #2)

将此代码用于 改造

 @POST("/user/register/")
Call<YourClass> createUser(@Body YourClass data);

而不是 YourClass 把你的 POJO class 有所有数据....

并为 POST 数据调用改造到服务器。

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://XXXXXXXXX.com")
.addConverterFactory(GsonConverterFactory.create())
.build();

YourClass you=new YourClass();
you.Key="new Key";//put all data like that in this class.

RegisterAPI api = retrofit.create(RegisterAPI.class);


Call<YourClass> call=api.createUser(you);
call.enqueue(new Callback<YourClass>() {}); 

如有任何错误欢迎随时询问....

删除这一行....

com.j2ml.casualtrack.pojo_Request.Data Data = new com.j2ml.casualtrack.pojo_Request.Data();

也试试这个

   data.setMobileNumber("+911234567890");
    data.setCountry("India");
    data.setCountryCode("IN");

而不是上面使用这个.....

    login.Data.setMobileNumber("+911234567890");
    login.Data.setCountry("India");
    login.Data.setCountryCode("IN");

享受编码......

如果你想发送一个对象(假设你的对象是像 Test class 这样的格式。这里的数据是另一个对象的列表,每个对象都有属性 [country ,country code,mobilenumber])。

class Test { 
@SerializedName("key")
int key;
@SerializedName("token")
string token;
@SerializedName("data")
ArrayList<Data> data; }

如果您 post 将此对象发送到服务器,它将按照您的要求 json 接收。

{
"Key":"",
"Token":"",
"Data":
 {
     "Country": "",
     "CountryCode":"",
     "MobileNumber":""
 }
}

你可以在那种情况下使用@Body。 在服务中你可以纠正这样的事情,

@POST(URL)
public Call<Test> postTest(@Body Test instanceOfTest);

希望对您有所帮助。