Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to post an JSON object using Retrofit 2. My JSON object is

{ "logTime" : "", "datas" : [ { "dat1": "1", " dat2": "", " dat3": "", " dat4": "", " dat5": ""
}, { "dat1": "1", " dat2": "", " dat3": "", " dat4": "", " dat5": ""
} ]}

I have tried using following code:

API Services

@FormUrlEncoded
@Headers({
        "Content-Type: application/json",
        "x-access-token: eyJhbGciOiJIU"
})
@POST("/api/employee/checkin")
Call<String> CHECKIN(@Body String data);

Activity Class

JSONStringer jsonStringer = null;
    try {
        jsonStringer=new JSONStringer().object().key("logTime").value("")
                .key("datas")
                .array()
                .object().key("dat1").value("1")
                .key("dat2").value("3")
                .key("dat3").value("5")
                .key("dat4").value("5")
                .endObject()
                .endArray()
                .endObject();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    ApiService service = retroClient.getApiService();

    Call<String> login = service.CHECKIN(String.valueOf(jsonStringer));

    login.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            dialog.dismiss();
            try {
                String val = response.body();


            } catch (Exception e) {
                e.getMessage();
            }
        }

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

        }
    });

I got "Error: No Retrofit annotation found. (parameter #2)" while using this code. Please help me. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

use GSON lib

add this dependency in build.gradle

compile 'com.google.code.gson:gson:2.8.0'

API Services

@Headers({
        "Content-Type: application/json",
        "x-access-token: eyJhbGciOiJIU"
})
@POST("/api/employee/checkin")
Call<String> CHECKIN(@Body String data);

Activity Class

try {

            JsonArray datas = new JsonArray();

            JsonObject object = new JsonObject();
            object.addProperty("dat1","1");
            object.addProperty("dat2", "");
            object.addProperty("dat3", "");
            object.addProperty("dat4", "");
            object.addProperty("dat5", "");

            datas.add(object);

            JsonObject req = new JsonObject();
            req.addProperty("logTime", "");
            req.addProperty("datas", new Gson().toJson(datas));


        } catch (Exception e) {
            e.printStackTrace();
        }



    ApiService service = retroClient.getApiService();

    Call<String> login = service.CHECKIN(String.valueOf(req));

    login.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            dialog.dismiss();
            try {
                String val = response.body();


            } catch (Exception e) {
                e.getMessage();
            }
        }

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

        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...