我无法从我的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}}
请告诉我这是什么错误
这似乎是来自服务器的警告说:已超过演示30000积分的每日限制。请使用特定于应用程序的帐户。不要使用演示帐户为您的应用程序....取决于演示帐户创建自己的特定帐户 –
如何删除此错误 –
在URL的末尾我看到username =“demo”是该帐户特定于您?反正这个帐户必须使用免费的网络服务......但他们可能有限制在特定的一天多少次,你可以打特定帐户的Web服务......也可以各种库json解析做谷歌搜索 –