如何在微调器中设置这种类型的json响应。作为回应我有两个json数组如何在不同的两个微调器中设置这两个数组。像拳头微调器有第一个数组值,如st1,st2和第二个微调器有st4,st5,st6的值。
我的Api回应是:
[
{
"status": "success",
"Data1": [
"st1",
"st2"
],
"Data2": [
"st3",
"st4",
"st4"
]
}
]
我试试
private void getdata(){
if (isNetworkAvailable()) {
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(800000);
final RequestParams params = new RequestParams();
client.post("here is my api url", new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode,
cz.msebera.android.httpclient.Header[] headers, String responseString,
Throwable throwable) {
Toast.makeText(MainActivity.this, responseString,
Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(int statusCode,
cz.msebera.android.httpclient.Header[] headers, String responseString) {
if (responseString != null) {
try {
JSONArray jaa = new JSONArray(responseString);
JSONObject json = jaa.getJSONObject(0);
String status = json.getString("status");
if (status.equals("success")) {
JSONArray jsonArray =
json.getJSONArray("Data1");
JSONArray jsonArray12 =
json.getJSONArray("Data2");
for (int i = 0; i < jsonArray.length(); i++) {
Data school = new Data();
school.setCurrency(jsonArray.getString(i));
school.setCrpto_currency(jsonArray12.getString(i));
btclist.add(school);
schoolNames.add(jsonArray.getString(i));
schoolNames12.add(jsonArray12.getString(i));
}
}
} catch (Exception e) {
e.printStackTrace();
}
spinner1.setAdapter(new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,
schoolNames));
spinner2.setAdapter(new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,
schoolNames12));
} else {
Toast.makeText(MainActivity.this, "data is not
available!", Toast.LENGTH_SHORT).show();
}
}
});
}else {
Toast.makeText(MainActivity.this, "No Internet connection",
Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
试试这个
try {
JSONArray jaa = new JSONArray(responseString);
JSONObject json = jaa.getJSONObject(0);
String status = json.getString("status");
if (status.equals("success")) {
JSONArray jsonArray = json.getJSONArray("Data1");
JSONArray jsonArray12 = json.getJSONArray("Data2");
for (int i = 0; i < jsonArray.length(); i++) {
schoolNames.add(jsonArray.getString(i));
}
for (int j = 0; j < jsonArray12.length(); j++) {
schoolNames12.add(jsonArray12.getString(j));
}
}
} catch (Exception e) {
e.printStackTrace();
}
spinner1.setAdapter(new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,
schoolNames));
spinner2.setAdapter(new ArrayAdapter<String>
(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,
schoolNames12));
答案 1 :(得分:0)
使用下面的代码检查代码中的任何错误包。确保列表和数组适配器中的数据类型相同..
// make sure data type are same in list and arrayAdapter
List<Data> schoolNames=new ArrayList<>();
ArrayAdapter<Data> dataArrayAdapter=new ArrayAdapter<Data>(this,MainActivity.this, android.R.layout.simple_spinner_dropdown_item,
schoolNames));
ArrayAdapter<Data2> dataArrayAdapter2=new ArrayAdapter<Data2>(this,MainActivity.this, android.R.layout.simple_spinner_dropdown_item,
schoolNames12));
spinner.setAdapter(dataArrayAdapter);
spinner2.setAdapter(dataArrayAdapter2);
dataArrayAdapter.notifyDataSetChanged();
dataArrayAdapter2.notifyDataSetChanged();