如何全局声明json id并使用全局变量将其传递给API请求?

时间:2017-01-03 11:49:49

标签: java android json

请告诉我如何将全部品牌的id全局声明为单个全局变量并将其作为http请求中的参数传递?

根据规定,我需要将所有下面提到的五个品牌的品牌ID声明为单个全局变量中的整数,并将其作为整数形式的参数传递给请求

我在下面提供了我的API响应:

{
  "status": 1,
  "data": [
    {
      "id": 1,----> brand id
      "name": "AUDI",
      "code": "AUDI",
      "image": "",
      "status": "1",
      "created_at": "2016-09-27 00:07:38",
      "updated_at": "2016-09-27 00:07:38"
    },
    {
      "id": 2-----> brand id
      "name": "Bravian Motor Works",
      "code": "BMW",
      "image": "",
      "status": "1",
      "created_at": "2016-09-27 00:07:58",
      "updated_at": "2016-09-27 00:07:58"
    },
    {
      "id": 3,---->brand id
      "name": "AB Volvo",
      "code": "VOLVO",
      "image": "",
      "status": "1",
      "created_at": "2016-09-27 00:08:36",
      "updated_at": "2016-09-27 00:08:36"
    },
    {
      "id": 4,-----> brand id
      "name": "Ford Motor Company",
      "code": "FORD",
      "image": "",
      "status": "1",
      "created_at": "2016-09-27 00:11:51",
      "updated_at": "2016-09-27 00:11:51"
    },
    {
      "id": 5,-----> brand id
      "name": "Maruti Suzuki",
      "code": "Maruti",
      "image": "",
      "status": "1",
      "created_at": "2016-09-27 00:12:14",
      "updated_at": "2016-09-27 00:12:14"
    }
  ],
  "msg": "success",
  "info": "data list"
}

我需要传递全局变量,如下所示:

@Override
        protected Void doInBackground(Void... arg0) {

            HttpClient client = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://garage.kaptastech.mobi/api/5k/master/vehicle");

            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(7);
            nameValuePair.add(new BasicNameValuePair("user_id","5"));
            nameValuePair.add(new BasicNameValuePair("registration_no", Regno));
            nameValuePair.add(new BasicNameValuePair("brand","?"));
            nameValuePair.add(new BasicNameValuePair("model", "5"));
            nameValuePair.add(new BasicNameValuePair("type", "1"));
            nameValuePair.add(new BasicNameValuePair("variant", "1"));
            nameValuePair.add(new BasicNameValuePair("year", Year));

我需要将它传递给&#34;?&#34;

3 个答案:

答案 0 :(得分:1)

将api响应保存在对象中。创建一个包含这些字段和getter + setter的类。

示例:

当您解析api响应时:

ArrayList<MyObject> listOfObjects = new ArrayList<MyObject>()
MyObject object = new MyObject();
object.setID(jsonObject.getString("id"));
object.setName(jsonObject.getString("name"));
// fill other fields
listOfObjects.add(object);

然后将listOfObjects保存在您想要的位置。例如经理。

然后你可以调用你需要信息的对象的getter。 例如:

// loop over all objects
for(MyObject object : Manager.getInstance().getObjects()) {
  // do whatever you want with these informations
  object.getID();
}

答案 1 :(得分:0)

将您的API响应保存在ArrayList中并在ListView中显示,然后在onItemClickListener上获取变量中的选定品牌ID: -

String selectedBrand;

lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        selectedBrand=yourArrayList.get(position).getBrandId();
        new yourAsyncTask(selectedBrand).execute();
    }
});

在您的异步任务中: -

class yourAsyncTask extends AsyncTask<String, String, String> {

    String brand_id;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    public yourAsyncTask(String brand_id)//change argument type accordingly
    {
       this.brand_id=brand_id;
    }
      //use brand_id as a parameter value
}

答案 2 :(得分:0)

创建单独的密钥类,您可以在其中全局存储密钥名称, 例如: 将类文件创建为

   public class Keys{public static final String KEY_NAME = "keyName";}

使用上面的类名作为:

Key.KEY_NAME