2015-09-24 199 views
2

我無法從我的code.please任何結果表明我的問題的解決方案,這是我的代碼,並在此先感謝如何使用pojoclass將JSon數組轉換爲Arraylist?

CODE: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); 
    } 
    } 
    } 

有時也出現此錯誤:

{「地位」:{「消息」:「30000個學分演示了漲停 超出請使用專用賬戶,不要使用 模擬賬戶爲您的應用。」 ,「價值」:18}}

請告訴我這是什麼錯誤

+0

這似乎是來自服務器的警告說:已超過演示30000積分的每日限制。請使用特定於應用程序的帳戶。不要使用演示帳戶爲您的應用程序....取決於演示帳戶創建自己的特定帳戶 –

+0

如何刪除此錯誤 –

+0

在URL的末尾我看到username =「demo」是該帳戶特定於您?反正這個帳戶必須使用免費的網絡服務......但他們可能有限制在特定的一天多少次,你可以打特定帳戶的Web服務......也可以各種庫json解析做谷歌搜索 –

回答

0

我想你應該檢查這個庫出Ion Library。 這將幫助你異步獲取JSON &輕鬆將其轉換爲POJO的ArrayList

1

我還不知道你爲什麼在這裏使用這個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()); 
.................................. 
.................................. 
+0

仍然沒有得到任何結果,我能夠從json獲取數據但無法將該數據放入列表視圖中,因此請提出解決方案 –

+0

您仍然不會將您的適配器中的任何數據,其空白。您應該將此ArrayList放入適配器中,以便它可以在ListView中使用。 – Ranjit

+0

我把數據放在arraylist ,然後從適配器使用它,但沒有得到任何結果 –

0
List<PojoClass> pojoClassList; 

pojoClassList = new ObjectMapper().readValue(jsonString,TypeFactory.collectionType(List.class, Employe.class)); 

建議ü閱讀GSON或Jakson庫。

0

將您的查看模式更改爲如下所示的實現。還可以將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

+0

已經做了這種方法現在我得到的是亂碼數據,但不是實際的數據 –

+0

我已經添加了修改後的代碼根據您的代碼中添加的觀察問題bcoz我覺得適配器沒有正確實現...只是嘗試一下,然後打印實際關於logcat的數據,並將其與視圖進行比較.... –

+0

進行這些更改後,我也沒有從適配器獲取任何數據。即使在logcat我沒有得到數據 –