[英]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.