使用AsyncHttpClient的列表视图
问题描述:
我想用ListView AsyncHttpClient
填充一些值为ListView
。使用AsyncHttpClient的列表视图
我不知道如何与AsyncHttpClient
public void getFavouriteWS(RequestParams params){
// Make RESTful webservice call using AsyncHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.example.com/getfavouriteonline",params ,new AsyncHttpResponseHandler() {
// When the response returned by REST has Http response code '200'
@Override
public void onSuccess(String response) {
try {
// JSON Object
JSONObject obj = new JSONObject(response);
String favourite_id = obj.getString("favourite_online_id");
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(getActivity(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
// When the response returned by REST has Http response code other than '200'
@Override
public void onFailure(int statusCode, Throwable error, String content) {
// When Http response code is '404'
if(statusCode == 404){
Toast.makeText(getActivity(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if(statusCode == 500){
Toast.makeText(getActivity(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else{
Toast.makeText(getActivity(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
我的JSON响应为做到这一点,
{"favourite_online_id":"3","favourite_online_url":"ddd","favourite_online_status":"0"}{"favourite_online_id":"2","favourite_online_url":"http:\/\/www.google.com","favourite_online_status":"0"}
如何填充在ListView
上述JSON
响应。
答
有几个步骤才能使其工作。首先,我们需要来自您的API的有效JSON。我通过myJSON建立了一个临时端点,您可以使用它来测试https://api.myjson.com/bins/ssc03
。按照这种格式:
[
{
"favourite_online_id": "3",
"favourite_online_url": "ddd",
"favourite_online_status": "0"
},
{
"favourite_online_id": "2",
"favourite_online_url": "http://www.google.com",
"favourite_online_status": "0"
}
]
既然JSON是有效的,我们可以解析它。我使用的ASyncHttpClient版本采用不同的参数,因此只关注内部的逻辑。
ArrayList<String> values = new ArrayList<>();
public void getFavouriteWS(RequestParams params){
// Make RESTful webservice call using AsyncHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://api.myjson.com/bins/ssc03",params ,new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
// JSON Array from byte[]
JSONArray arr = new JSONArray(new String(responseBody));
//loop each object in the array
for(int i = 0; i< arr.length(); i++) {
// get value needed
String favourite_id = arr.getJSONObject(i).getString("favourite_online_id");
// add value to arrayList
values.add(favourite_id);
}
// call method to show results
populateList();
} catch (JSONException e) {
// TODO Auto-generated catch block
Toast.makeText(MainActivity.this, "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// When Http response code is '404'
if(statusCode == 404){
Toast.makeText(MainActivity.this, "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if(statusCode == 500){
Toast.makeText(MainActivity.this, "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else{
Toast.makeText(MainActivity.this, "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
为了测试目的,我在同一个文件中做了所有这些。如果你想把你的API调用封装到一个单独的文件中,你需要创建一个从API调用到你的活动或片段的回调。在你的列表视图引用文件内做这样的事情:
public void populateList(){
//create simple adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
//assign adapter to listview
listView.setAdapter(adapter);
}
你json无效 –