user2849146 user2849146 - 2 months ago 35
JSON Question

Retrofit2: android POST parameters and and GET json array

I am trying to parse a JSON file with Retrofit2
want to add a body (parameters)

URL like : domain.com/list-all/


{"userId": 7446,"pages":25}


and JSON response is look like this format

{
"todaysList": [
{
"automatic": false,
"city": "Trivandrum",
"countryName": "IND",
"firstName": "Sarojam",
"flagExists": true,
"flagPath": "url",
"friendReqStatus": 0,
"fullName": " M",
"gender": "F",
"inRequest": false,
"lastName": "M",
"optOut": false,
"outRequest": false,
"position": 1,
"profileExist": false,
"todaysWinner": false,
"userId": 20726,

},
{
"automatic": false,
"city": "Trivandrum",
"countryName": "IND",
"firstName": "Sarojam",
"flagExists": true,
"flagPath": "url",
"friendReqStatus": 0,
"fullName": " M",
"gender": "F",
"inRequest": false,
"lastName": "M",
"optOut": false,
"outRequest": false,
"position": 1,
"profileExist": false,
"todaysWinner": false,
"userId": 20726
},
....
}


How I parse it with
POST
with two body params using
Retrofit2

Answer

I was based Retrofit2 solution to your problem, please note this!

First, in builde.gradle file, add the following dependencies:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Then initialize Retrofit:

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

GsonConverterFactory will automatically convert JSON string to Java class objects this point is very important!

Next, create a body of content request data objects:

package com.test.xujianzhe.testandroid.retrofit.network_bean;

/**
 * Created by xujianzhe on 16/9/10.
 */

public class TestRequestBean {

/**
 * userId : 7446
 * pages : 25
 */

private int userId;
private int pages;

public int getUserId() {
    return userId;
}

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

public int getPages() {
    return pages;
}

public void setPages(int pages) {
    this.pages = pages;
}
}

Next, create the server returns data structures:

package com.test.xujianzhe.testandroid.retrofit.network_bean;

 import java.util.List;

/**
 * Created by xujianzhe on 16/9/10.
 */

 public class TestEntity {

/**
 * automatic : false
 * birthdayPhotoUrl : url
 * city : Trivandrum
 * countryName : IND
 * donated : false
 * expressionId : 8435
 * expressionText : joidj
 * firstName : Sarojam
 * flagExists : true
 * flagPath : url
 * friendReqStatus : 0
 * fullName :  M
 * gender : F
 * inRequest : false
 * lastName : M
 * numberOfTimesDonation : 0
 * optOut : false
 * outRequest : false
 * position : 1
 * profileExist : false
 * todaysWinner : false
 * userId : 20726
 * voted : false
 * votes : 77545
 */

private List<TodaysListBean> todaysList;

public List<TodaysListBean> getTodaysList() {
    return todaysList;
}

public void setTodaysList(List<TodaysListBean> todaysList) {
    this.todaysList = todaysList;
}

public static class TodaysListBean {
    private boolean automatic;
    private String birthdayPhotoUrl;
    private String city;
    private String countryName;
    private boolean donated;
    private int expressionId;
    private String expressionText;
    private String firstName;
    private boolean flagExists;
    private String flagPath;
    private int friendReqStatus;
    private String fullName;
    private String gender;
    private boolean inRequest;
    private String lastName;
    private int numberOfTimesDonation;
    private boolean optOut;
    private boolean outRequest;
    private int position;
    private boolean profileExist;
    private boolean todaysWinner;
    private int userId;
    private boolean voted;
    private int votes;

    public boolean isAutomatic() {
        return automatic;
    }

    public void setAutomatic(boolean automatic) {
        this.automatic = automatic;
    }

    public String getBirthdayPhotoUrl() {
        return birthdayPhotoUrl;
    }

    public void setBirthdayPhotoUrl(String birthdayPhotoUrl) {
        this.birthdayPhotoUrl = birthdayPhotoUrl;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public boolean isDonated() {
        return donated;
    }

    public void setDonated(boolean donated) {
        this.donated = donated;
    }

    public int getExpressionId() {
        return expressionId;
    }

    public void setExpressionId(int expressionId) {
        this.expressionId = expressionId;
    }

    public String getExpressionText() {
        return expressionText;
    }

    public void setExpressionText(String expressionText) {
        this.expressionText = expressionText;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public boolean isFlagExists() {
        return flagExists;
    }

    public void setFlagExists(boolean flagExists) {
        this.flagExists = flagExists;
    }

    public String getFlagPath() {
        return flagPath;
    }

    public void setFlagPath(String flagPath) {
        this.flagPath = flagPath;
    }

    public int getFriendReqStatus() {
        return friendReqStatus;
    }

    public void setFriendReqStatus(int friendReqStatus) {
        this.friendReqStatus = friendReqStatus;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public boolean isInRequest() {
        return inRequest;
    }

    public void setInRequest(boolean inRequest) {
        this.inRequest = inRequest;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getNumberOfTimesDonation() {
        return numberOfTimesDonation;
    }

    public void setNumberOfTimesDonation(int numberOfTimesDonation) {
        this.numberOfTimesDonation = numberOfTimesDonation;
    }

    public boolean isOptOut() {
        return optOut;
    }

    public void setOptOut(boolean optOut) {
        this.optOut = optOut;
    }

    public boolean isOutRequest() {
        return outRequest;
    }

    public void setOutRequest(boolean outRequest) {
        this.outRequest = outRequest;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public boolean isProfileExist() {
        return profileExist;
    }

    public void setProfileExist(boolean profileExist) {
        this.profileExist = profileExist;
    }

    public boolean isTodaysWinner() {
        return todaysWinner;
    }

    public void setTodaysWinner(boolean todaysWinner) {
        this.todaysWinner = todaysWinner;
    }

    public int getUserId() {
        return userId;
    }

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

    public boolean isVoted() {
        return voted;
    }

    public void setVoted(boolean voted) {
        this.voted = voted;
    }

    public int getVotes() {
        return votes;
    }

    public void setVotes(int votes) {
        this.votes = votes;
    }
}
}

create the interface :

package com.test.xujianzhe.testandroid.retrofit;

import com.test.xujianzhe.testandroid.retrofit.network_bean.TestEntity;
import com.test.xujianzhe.testandroid.retrofit.network_bean.TestRequestBean;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

/**
 * Created by xujianzhe on 16/9/10.
 */

public interface ITestRetrofit {

     @POST
     Call<TestEntity> testOperation(@Body TestRequestBean requestBean);
}

Finally, create the request:

TestRequestBean testRequestBean = new TestRequestBean();
    testRequestBean.setPages(2);
    testRequestBean.setUserId(2);
     retrofit.create(ITestRetrofit.class).testOperation(testRequestBean).enqueue(new Callback<TestEntity>() {
        @Override
        public void onResponse(Call<TestEntity> call,    Response<TestEntity> response) {
            // response
        }

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

        }
    });

Complete working code is as follows:

package com.test.xujianzhe.testandroid.retrofit.network_bean;

import android.content.Context;

import com.test.xujianzhe.testandroid.retrofit.ITestRetrofit;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xujianzhe on 16/9/10.
 */

public class RetrofitWork {

public static void requestOperation(Context context) {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.github.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    TestRequestBean testRequestBean = new TestRequestBean();
    testRequestBean.setPages(2);
    testRequestBean.setUserId(2);
    retrofit.create(ITestRetrofit.class).testOperation(testRequestBean).enqueue(new Callback<TestEntity>() {
        @Override
        public void onResponse(Call<TestEntity> call, Response<TestEntity> response) {
            // response
        }

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

        }
    });
}

}

I hope the above content can solve your problems!

Comments