简体   繁体   中英

How to retrieve a value from list in retrofit

here my module

public class SPSOnHand { 
    @SerializedName("ITEM_CODE")
    @Expose
    private String ITEM_CODE;
    @SerializedName("ITEM_DESCRIPTION")
    @Expose
    private String ITEM_DESCRIPTION;

    public String getITEM_CODE() {
        return ITEM_CODE;
    }

    public void setITEM_CODE(String item_code) {
        ITEM_CODE = item_code;
    }

    public String getITEM_DESCRIPTION() {
        return ITEM_DESCRIPTION;
    }

    public void setITEM_DESCRIPTION(String item_description) {
        ITEM_DESCRIPTION = item_description;
    }

here my rest service class

public class RestService {

    private static final String URL = "http://localhost:58364/";
    private retrofit.RestAdapter restAdapter;
    //private InstituteService apiService;
    private SPInventoryService apiService;

    public RestService()
    {
        restAdapter = new retrofit.RestAdapter.Builder()
                .setEndpoint(URL)
                .setLogLevel(retrofit.RestAdapter.LogLevel.FULL)
                .build();

        apiService = restAdapter.create(SPInventoryService.class);
    }

    public SPInventoryService getService()
    {
        return apiService;
    }

here my service

@GET("/api/SPSOnHand/itemcode/{item_code}")
public void getItemCode(@Path("item_code") String itemcode, 
Callback<ArrayList<SPSOnHand>>callback);

here my main activity class

        String itemcode;
        Intent intent = getIntent();
        itemcode =intent.getStringExtra("itemcode");

            restService.getService().getItemCode(itemcode, new Callback<ArrayList<SPSOnHand>>() {

                @Override
                public void success(ArrayList<SPSOnHand> spsOnHands, Response response) {
                    item_description.setText(String.valueOf(spsOnHands.get(0)));

                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(AirJet.this, error.getMessage().toString(), 
                Toast.LENGTH_LONG).show();

                }
            });

在此处输入图像描述

I want that item_description to contain a word like this

"ID": 167,
"ITEM_CODE": "3020240002",
"ITEM_DESCRIPTION": "CONTROLLER LME22.331c2 (CONTROL BOX)"

thanks in advance, I'm still a newbie in retrofit

Because of String.valueOf(spsOnHands.get(0)) whole item of list converted to String so that's why you are getting wrong format of text.

Use below code for set text in textview:

 int id = 0+1;
 String itemCode = arrayList.get(0).getITEM_CODE();
 String itemDescription = arrayList.get(0).getITEM_CODE();
 item_description.setText("\"ID\":"+ id+" \n\"ITEM_CODE\":" + itemCode+" \n\"ITEM_DESCRIPTION\":"+itemDescription);

You can change format of text in textview according to your requirement.

In your case, you receive ArrayList . And if you want to get the item from List then you have to pass index and get value List , like below. It will give you ITEM_DESCRIPTION for the 0th index of your List .

restService.getService().getItemCode(itemcode, new Callback<ArrayList<SPSOnHand>>() {

                @Override
                public void success(ArrayList<SPSOnHand> spsOnHands, Response response) {
                    item_description.setText(String.valueOf(spsOnHands.get(0).ITEM_DESCRIPTION));

                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(AirJet.this, error.getMessage().toString(), 
                Toast.LENGTH_LONG).show();

                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM