我無法從我的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解析做谷歌搜索 –