[英]Android fragment with web services
I am invoking the web service inside fragment. 我正在片段中调用Web服务。 The Bankranking list from web service has the value and I am able to print.
来自Web服务的Bankranking列表具有该值,并且我可以打印。 But after the method
onSuccess
, if the print the list I am getting array index out of bound Exception. 但是在方法
onSuccess
,如果打印列表,我将得到数组索引超出范围的异常。
public class BankRatingOffsiteIndustry extends Fragment {
List<BankRankingDTO> bankRanking = new ArrayList<>();
@Override
public void onStart() {
super.onStart();
RequestParams params = new RequestParams();
params.add("date", "2017-12-31");
params.add("currency", "EL");
invokeWebS(params);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.bankrankinginfo, container, false);
return v;
}
public void invokeWebS(RequestParams params) {
final AsyncHttpClient client = new AsyncHttpClient();
client.get("url", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
ObjectMapper mapperObject = new ObjectMapper();
System.out.println("Input Json: " + response.toString());
bankRanking = mapperObject.readValue(response.toString(), mapperObject.getTypeFactory().constructCollectionType(List.class, BankRankingDTO.class));
/* setResultList(resultList);*/
Log.i("exit",bankRanking.get(0).getInstitutionName());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Log.i("values",bankRanking.get(0).getInstitutionName());
}
Check your bankRanking
list size if is not empty then it work. 检查您的
bankRanking
列表大小,如果不为空,则工作正常。 And you can also used for each loop to print list like below code: 而且,您还可以将其用于每个循环以打印列表,如下所示:
if (!bankRanking.isEmpty()){
for(BankRankingDTO object:bankRanking) {
Log.i("values", object.getInstitutionName());
}
}
You are trying to show a log when the web service call is yet to return. 您试图在Web服务调用尚未返回时显示日志。 The web service call is
Async
. Web服务调用是
Async
。 So you need to wait for the values to be returned to the application and then print the log. 因此,您需要等待这些值返回给应用程序,然后打印日志。
Just move the following line inside your onSuccess
function. 只需
onSuccess
下行移动到onSuccess
函数中即可。
Log.i("values",bankRanking.get(0).getInstitutionName());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.