[英]how to convert JSon array to Arraylist with the help of pojoclass?
我无法从我的代码中得到任何结果。请为我的问题提出解决方案,这是我的代码,并提前致谢
代码:Activity.main
public class MainActivity extends Activity {
ArrayList<detail> country = new ArrayList<detail>();
class detail{
public String toponymName;
public String countrycode;
public int population;
public String wikipedia;
}
Adapter ad = null;
static ArrayList<string> resultrow;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new HttpAsyncTask().execute("http://api.geonames.org/citiesJSON? north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo");
ListView mylistview = (ListView)findViewById(R.id.mylistview);
ad = new Adapter();
mylistview.setAdapter(ad);
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null){
Log.e("Line",line);
result += line;
}
inputStream.close();
return result;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
String strJson = result;
try {
JSONObject jsonRootObject = new JSONObject(strJson);
JSONArray jsonArray = jsonRootObject.optJSONArray("geonames");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
detail resultrow = new detail();
resultrow.toponymName =jsonObject.getString("toponymName");
resultrow.countrycode =jsonObject.getString("countrycode");
resultrow.wikipedia =jsonObject.getString("wikipedia");
resultrow.population =jsonObject.getInt("population");
country.add(resultrow);
}
}
catch (JSONException e) {e.printStackTrace();}
}
}
class Adapter extends ArrayAdapter<detail>{
Adapter(){
super(MainActivity.this,android.R.layout.simple_list_item_1,country);
}
public View getview(int position,View convertview,ViewGroup parent){
viewHolder holder;
if(convertview==null){
LayoutInflater inflater = getLayoutInflater();
convertview=inflater.inflate(R.layout.row, null);
holder = new viewHolder(convertview);
convertview.setTag(holder);
}
else{
holder=(viewHolder)convertview.getTag();
}
holder.populateFrom(country.get(position));
return convertview;
}
}
class viewHolder{
public TextView toponymName=null;
public TextView countrycode=null;
public TextView wikipedia=null;
public TextView population=null;
viewHolder(View row){
toponymName =(TextView)row.findViewById(R.id.toponymName);
countrycode =(TextView)row.findViewById(R.id.countrycode);
wikipedia =(TextView)row.findViewById(R.id.wikipedia);
population =(TextView)row.findViewById(R.id.population);
}
//not able to populate this block
void populateFrom(detail r){
toponymName.setText(r.toponymName);
countrycode.setText(r.countrycode);
wikipedia.setText(r.wikipedia);
population.setText(r.population);
}
}
}
有时也会收到此错误:
{"status":{"message":"已超过每日 30000 积分的演示限制。请使用应用程序专用帐户。请勿将演示帐户用于您的应用程序。","value":18}}
请告诉我这是什么错误
我仍然不知道你为什么在这里使用这个 pojo 概念。
无论如何,我只能建议你在课堂上添加一些 getter/setter。 喜欢
class detail{
public String toponymName;
public String countrycode;
public int population;
public String wikipedia;
public String getToponymName() {
return toponymName;
}
public void setToponymName(String toponymName) {
this.toponymName = toponymName;
}
public String getCountrycode() {
return countrycode;
}
public void setCountrycode(String countrycode) {
this.countrycode = countrycode;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public String getWikipedia() {
return wikipedia;
}
public void setWikipedia(String wikipedia) {
this.wikipedia = wikipedia;
}
}
它将帮助您从 JSON 字符串设置和获取特定数据。
就像是:
JSONObject jsonObject = jsonArray.getJSONObject(i);
detail resultrow = new detail();
resultrow.setToponymName(jsonObject.getString("toponymName");
..........
.......
country.add(resultrow);
当你想设置它时::就去做
toponymName.setText(r.getToponymName());
..................................
..................................
我想你应该检查这个库Ion Library 。 它将帮助您异步获取 JSON 并轻松将其转换为 POJO 的ArrayList
。
List<PojoClass> pojoClassList;
pojoClassList = new ObjectMapper().readValue(jsonString,TypeFactory.collectionType(List.class, Employe.class));
建议你阅读 Gson 或 Jakson 库。
将您的视图持有者模式更改为实现,如下所示。 并且还将 Adapter 的构造函数更改为如下所示的任一实现。
class Adapter extends ArrayAdapter<detail>{
//below variables are optional
List<detail> actList;
Context context;
Adapter(Context context, Context context, int resource, List<detail> actList){
super(context,resource,actList);
//below variables are optional
this.context = context;
this.actList = actList;
}
---OR----
Adapter(Context context, Context context, int resource){
super(context,resource);
//below variables are optional
this.context = context;
}
public View getview(int position,View convertview,ViewGroup parent){
ViewHolder holder;
final detail r = actList.get(position);
if(convertview==null){
LayoutInflater inflater = getLayoutInflater();
convertview=inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.toponymName =(TextView)convertview.findViewById(R.id.toponymName);
holder.countrycode =(TextView)convertview.findViewById(R.id.countrycode);
holder.wikipedia =(TextView)convertview.findViewById(R.id.wikipedia);
holder.population =(TextView)convertview.findViewById(R.id.population);
convertview.setTag(holder);
} else {
holder=(ViewHolder)convertview.getTag();
}
if(r!= null) {
holder.toponymName.setText(r.toponymName);
holder.countrycode.setText(r.countrycode);
holder.wikipedia.setText(r.wikipedia);
holder.population.setText(r.population);
}
return convertview;
}
}
static class ViewHolder{
TextView toponymName;
TextView countrycode;
TextView wikipedia;
TextView population;
}
在初始化适配器时,根据使用的构造函数执行以下操作。
ad = new Adapter(this, R.layout.row, actlist)
OR
ad = new Adapter(this, R.layout.row)
基于新评论的更新
首先是错误
1)我观察到您正在做的一件事是启动 asynctask 并将适配器设置为列表,而在 onCreate 方法中不获取或不获取数据保证,因此您的列表可能为空……您可以这样做,但是您需要更新适配器中的列表并通知适配器更改的数据集
2)其次,我在您的代码中观察到许多列表对象,这有点令人困惑。此外,您还使用了很多静态的东西...原因我不知道; 但我觉得这是多余的。 像GET(String url)
[---它应该被命名为 get(String url)]和convertInputStreamToString(InputStream inputStream)
可以在 HttpAsyncTask bcoz 中创建,它仅供该线程使用。
3)你也没有使用任何东西来表明数据正在从服务器加载
如前所述,我将在适配器更改中添加更多更改。
1)为ListView制作实例变量如下,并在onCreate方法中更改也为setAdapter创建方法
Adapter ad = null;
ListView listView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mylistview = (ListView)findViewById(R.id.mylistview);
new HttpAsyncTask().execute("http://api.geonames.org/citiesJSON? north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo");
}
public void setListAdapter(){
if(ad == null){
ad = new Adapter(this, R.layout.row)
mylistview.setAdapter(ad);
} else {
ad.notifydatasetchanged();
}
}
然后在 postExecute 你需要调用这个 setListAdapter 方法....
@Override
protected void onPostExecute(String result) {
//your code
try{
//your code
setListAdapter()
}catch (JSONException e) {e.printStackTrace();//use log i.e Log.e(TAG,msg)}
}
2) 在您的应用程序中使用ProgressBar view
来通知用户加载正在进行中。 您可以使其可见并在 PreExecute 中启动并在 PostExecute 中停止和可见性消失
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.