2014-04-07 15 views
0

以下是在瀏覽器上顯示的resful web服務。我需要讀取這些數據信息andoird作爲客戶端列表[AsynTask] [Restful Client(Android用戶)] [Spring框架]從doInBackground到onPostExecute的類的ArrayList

ArrayList<PickerItemDetail> 
//content of http://127.0.0.1:8080/picking 
[ 
{"pickerBatch":1,"sku":"FL45678976543","name":"Coke 1000L","image":"coke.jpg","qty":"8","pickingStatus":"picked","pickedBy":"John","pickedOn":"02/04/2014","locked":"0"}, 

{"pickerBatch":2,"sku":"FL45678543675","name":"Mineral Water 50L","image":"drinkingWater.jpg","qty":"5","pickingStatus":"picked","pickedBy":"Roy","pickedOn":"02/04/2014","locked":"0"} 
] 

對於機器人側

public class MainActivity extends ListActivity { 
@Override 
protected void onStart() { 
    super.onStart(); 
    new HttpRequestTask().execute(); 
} 

private class HttpRequestTask extends AsyncTask<Void, Void, ArrayList<PickerItemDetail>> { 
    @Override 
    protected ArrayList<PickerItemDetail> doInBackground(Void... params) { 
     try { 
      final String url = "http://127.0.0.1:8080/picking"; 
      RestTemplate restTemplate = new RestTemplate(); 
      restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());     
      ArrayList<PickerItemDetail> item = restTemplate.getForObject(url, ArrayList.class); 

      return item; 
     } catch (Exception e) { 
      Log.e("MainActivity", e.getMessage(), e); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<PickerItemDetail> item) { 
     TextView sku = (TextView) findViewById(R.id.sku); 
     TextView qty = (TextView) findViewById(R.id.qty); 
     TextView name = (TextView) findViewById(R.id.skuname); 
     sku.setText(item.get(0).getSku()); 
     qty.setText(item.get(0).getQty()); 
     name.setText(item.get(0).getName()); 


    } 

} 

} 

我想讀取來自web服務的數據的列表,然後顯示它作爲在機器人側的表。 我如何有一個適當的代碼來循環ArrayList,我認爲這些代碼不完全正確的方式,但沒有想法繼續/使它非常有效。

onPostExecute(ArrayList<>){ item.get(0).getXXX} 

希望有人建議 ,謝謝

+0

需要解析JSON – Raghunandan

回答

1

我不知道正是你需要的,但如果你想顯示列表視圖,您可以用 這樣去....你postexecut =錫安 加這個函數loadData();

loadDate()函數會像

private void loadData() { 
      try { 
       // looping through All Contacts 
       contactList.clear(); 
       for (int i = 0; i < contacts.length(); i++) { 
        JSONObject c = contacts.getJSONObject(i); 
        // Storing each json item in variable 
        String pickerBatch = c.getString("pickerBatch"); 
        String sku = c.getString(sku); 


        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        map.put("pickerBatch", pickerBatch); 
        map.put("sku", sku); 
        // adding HashList to ArrayList 
        contactList.add(map); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      ListAdapter adapter = new MySimpleAdapter(Activity.this, 
        contactList, R.layout.list_item, new String[] { 
        pickerBatch, sku }, new int[] { 
        R.id.pickerBatch, R.id.sku }); 

      setListAdapter(adapter); 
      // 

      ///
      // selecting single ListView item 

     } 

在上述例子中contactList是你的ArrayList ...希望它會工作

相關問題