我在下面有一个json数组,并且想要选择相应的id作为数字并用文本填充微调器,当从spinner中选择一个也是动态的选项时,也就是在Spinner中显示的json数组并发送id到服务器,任何帮助请guzs
{
"status": "success",
"message": "banks Fetched",
"data": {
"214": "FIRST CITY MONUMENT BANK PLC",
"215": "UNITY BANK PLC",
"221": "STANBIC IBTC BANK PLC",
"232": "STERLING BANK PLC",
"301": "JAIZ BANK",
"304": "Stanbic Mobile",
"305": "PAYCOM",
"307": "Ecobank Mobile",
"309": "FBN MOBILE",
"311": "Parkway",
"315": "GTBank Mobile Money",
"322": "ZENITH Mobile",
"323": "ACCESS MOBILE",
"401": "Aso Savings and Loans",
"044": "ACCESS BANK NIGERIA",
"014": "AFRIBANK NIGERIA PLC",
"063": "DIAMOND BANK PLC",
"050": "ECOBANK NIGERIA PLC",
"084": "ENTERPRISE BANK LIMITED",
"070": "FIDELITY BANK PLC",
"011": "FIRST BANK PLC",
"058": "GTBANK PLC",
"030": "HERITAGE BANK",
"082": "KEYSTONE BANK PLC",
"076": "SKYE BANK PLC",
"068": "STANDARD CHARTERED BANK NIGERIA LIMITED",
"032": "UNION BANK OF NIGERIA PLC",
"033": "UNITED BANK FOR AFRICA PLC",
"035": "WEMA BANK PLC",
"057": "ZENITH BANK PLC"
}
}

答案 0 :(得分:1)
所以尝试使用volley库:
private void sendjsonRequest(){
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.GET,"serverlink",null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
String[] banks=parsejsonResponse(response);
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, banks);
s.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonArrayRequest);
}
private String[] parsejsonResponse(JSONArray array){
String[] banks;
if (array!= null){
JSONArray data=array.getJSONArray("data");
for (int i=0;i<data.length();i++){
try {
JSONObject bank=data.getJSONObject(i);
String id=bank.getString("id");
String bankname=bank.getString("bankname")
banks.add(bankname);
}
} catch (JSONException e) {
}
}
}
return banks;
}
格式json就像这样的例子:
[{
"status": "success",
"message": "banks Fetched",
"data": [
{
"id": 214,
"bankname": "FIRST CITY MONUMENT BANK PLC"
},
{
"id": 215,
"bankname": "UNITY BANK PLC"
},
{
"id": 221,
"bankname": "STANBIC IBTC BANK PLC"
},
{
"id": 232,
"bankname": "STERLING BANK PLC"
},
{
"id": 301,
"bankname": "JAIZ BANK"
},
{
"id": 304,
"bankname": "Stanbic Mobile"
},
{
"id": 305,
"bankname": "PAYCOM"
}
]
}]
在 oncreate 中只需cal sendjsonrequest()方法