2015-09-24 187 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我没有得到数据 –