我正在开发一个使用eclipse和retrofit的应用程序,旨在获取JSON对象数组并将其呈现在listview中。我的web服务工作正常。我正在使用retrofit2获取POJO类,如下所示:
public static UnicsAgencyApi getUnicsAgencyApi() {
if (sUnicsAgencyApi == null) {
retrofit = new Retrofit.Builder().baseUrl(ENDPOINT_URL).addConverterFactory(GsonConverterFactory.create())
.build();
sUnicsAgencyApi = retrofit.create(UnicsAgencyApi.class);
}
return sUnicsAgencyApi;
}
public interface UnicsAgencyApi {
@GET("api/uconnectservice/AllAgency")
Call<ArrayList<AgencyModel>> getStreams();
}
这是我打电话的地方:
public void ShowAllUnicsAgencyInfo() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(Homepage.this);
final View promptView = layoutInflater.inflate(R.layout.view_agency_list, null);
listView2 = (ListView) promptView.findViewById(R.id.list);
// GET DATA FROM WEBSERVICE
ArrayList<AgencyModel> AgencyList = null;
AgencyList = DownloadAgencyData();
**AgencyListAdapter mAdapter = new AgencyListAdapter(this, AgencyList);**
listView2.setAdapter(mAdapter);
// ListView Item Click Listener
listView2.setOnItemSelectedListener(new CustomOnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT)
.show();
}
});
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Homepage.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder.setCancelable(false).setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
public ArrayList<AgencyModel> DownloadAgencyData() {
RestApi.getUnicsAgencyApi().getStreams().enqueue(new Callback<ArrayList<AgencyModel>>() {
@Override
public void onFailure(Call<ArrayList<AgencyModel>> arg0, Throwable arg1) {
// TODO Auto-generated method stub
Log.e("Error in parsing", arg0.toString());
}
@Override
public void onResponse(Call<ArrayList<AgencyModel>> AgencyModelData,
Response<ArrayList<AgencyModel>> response) {
// TODO Auto-generated method stub
mstreamData = new ArrayList<AgencyModel>();
// ADD TO List here!!!!!!!!
// Log.e("Response", "" + response.body().size());
**mstreamData.addAll(response.body());**
}
});
return mstreamData;
}
但是我不断得到的错误来自自定义数组适配器getCount()方法,通过以下Logcat:
01-03 02:48:41.615: E/test(30453): Exception
01-03 02:48:41.635: E/AndroidRuntime(30453): FATAL EXCEPTION: main
01-03 02:48:41.635: E/AndroidRuntime(30453): Process: com.nickSoft.unics_alpha, PID: 30453
01-03 02:48:41.635: E/AndroidRuntime(30453): java.lang.NullPointerException
01-03 02:48:41.635: E/AndroidRuntime(30453): at com.nickSoft.unics_alpha.AgencyListAdapter.getCount(AgencyListAdapter.java:26)
01-03 02:48:41.635: E/AndroidRuntime(30453): at android.widget.ListView.setAdapter(ListView.java:480)
01-03 02:48:41.635: E/AndroidRuntime(30453): at com.nickSoft.unics_alpha.Homepage.ShowAllUnicsAgencyInfo(Homepage.java:546)
我似乎无法确定我哪里出错,任何可能是错误的指导或解决此问题的更好方法都非常感谢,欢呼
答案 0 :(得分:0)
RestApi.getUnicsAgencyApi()。getStreams()。enqueue方法更可能在thread上工作。所以下载...方法返回数据将不正确。你可以在onResponse方法中调用bakc结果。
答案 1 :(得分:0)
我猜你的AgencyList没有实例化,或者即使在调用
之后也指向nullAgencyList = DownloadAgencyData();
在该行添加断点以进行调试。
由于您的列表视图取决于来自网络服务的数据,因此在调用setAdapter();
之前检查是否收到数据是好的 -
if(AgencyList !=null){
**AgencyListAdapter mAdapter = new AgencyListAdapter(this, AgencyList);**
listView2.setAdapter(mAdapter);
} else{ Log.d(TAG,"AgencyList is null");
编辑:看看这是否有帮助 设置多次访问数据的尝试。
for( int retry=0; AgencyList!=null && retry <10; retry++){
AgencyList = DownloadAgencyData();
}