I am unable to get any result from my code.please suggest the solution for my problem, here is my code and thanks in Advance
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);
}
}
}
sometimes also get this error :
{"status":{"message":"the daily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.","value":18}}
please tell me what is this error
I still don't know that why you are using this pojo concept here.
Anyway , I can only suggest you do add some getter/setter in side the class. like
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;
}
}
It will help you to set and get particular data from JSON String.
Something like:
JSONObject jsonObject = jsonArray.getJSONObject(i);
detail resultrow = new detail();
resultrow.setToponymName(jsonObject.getString("toponymName");
..........
.......
country.add(resultrow);
When you want to set it:: Just do
toponymName.setText(r.getToponymName());
..................................
..................................