[
{
"value": "--NA--",
"id": "251"
},
{
"value": "Adilabad",
"id": "1346"
},
{
"value": "Yavatmal",
"id": "1478"
}
]
如何从 json webservice 将上面的 json 解析为 android spinner。
我也经历了许多解决方案,但没有到达那里。我试过这个,但它不适合我
public class SpinnerDemo extends Activity {
//JSON node names
private static final String TAG_DATA = "data";
private static final String TAG_CREATED_AT = "created_at";
private static final String TAG_ID_DRINK = "id";
private static final String TAG_NAME = "name";
private static final String TAG_PRICE = "price";
private static final String TAG_UPDATED_AT = "updated_at";
private static final String MAP_API_URL = "http://domainname.com/webservice/WSHMS.asmx?op=GetCityList";
private BackGroundTask bgt;
Spinner drinkField;
ArrayList<Drink> drinkList = new ArrayList<Drink>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_main);
buildDrinkDropDown();
}
private void buildDrinkDropDown() {
List<NameValuePair> apiParams = new ArrayList<NameValuePair>(1);
apiParams.add(new BasicNameValuePair("call", "drinkList"));
bgt = new BackGroundTask(MAP_API_URL, "GET", apiParams);
try {
JSONObject drinkJSON = bgt.execute().get();
// Getting Array of drinks
JSONArray drinks = drinkJSON.getJSONArray(TAG_DATA);
// looping through All drinks
for (int i = 0; i < drinks.length(); i++) {
JSONObject d = drinks.getJSONObject(i);
// Storing each json item in variable
String id = d.getString(TAG_ID_DRINK);
String createdAt = d.getString(TAG_CREATED_AT);
String updatedAt = d.getString(TAG_UPDATED_AT);
String price = d.getString(TAG_PRICE);
String name = d.getString(TAG_NAME);
// add drink
drinkList.add(new Drink( createdAt ,id, name, price,updatedAt ));
}
我使用了这段代码但没有得到它......请帮助我