我正在尝试使用ListView
填充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"}
如何在JSON
填充上述ListView
响应。
答案 0 :(得分:1)
要实现这一目标,需要采取一些措施。首先,我们需要来自您的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调用创建回调。在listview参考文件中,执行以下操作:
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);
}