2

Basically i am trying to make an weather app. And i tried to retrieved some data (lets say lat and lon from crood object) but whenever i run the application it doesn't show me any error but it show in logcat an exception which says "No value for crood". I have tried every resource that i had but i am unable to display data to textviews.

this is the json i want want to target

{"coord":{"lon":-0.13,"lat":51.51},"weather":      [{"id":721,"main":"Haze","description":"haze","icon":"50d"},     {"id":501,"main":"Rain","description":"moderate rain","icon":"10d"},  {"id":311,"main":"Drizzle","description":"rain and drizzle","icon":"09d"}],"base":"stations","main":{"temp":286.15,"pressure":1013,"humidity":87,"temp_min":286.15,"temp_max":286.15},"visibility":10000,"wind":{"speed":3.1,"deg":200},"clouds":{"all":75},"dt":1445700000,"sys":  {"type":1,"id":5089,"message":0.0153,"country":"GB","sunrise":1445668823,"sunset":1445705286},"id":2643743,"name":"London","cod":200}

This is how i am doing it

            String finalJson=buffer.toString();

            JSONObject parentObject = new JSONObject(finalJson);
            JSONObject jsonObject =parentObject.getJSONObject("crood");

If somebody help me with this logic how to parse it. I want to get lat,lon,name,description,temp and pressure out of this data and i want to show it in textviews.

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74

3 Answers3

1

Problems was with key. I was using wrong key. :3 crood instead of coord

Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
0
  1. Create the Model, declare what u want in set and get.
  2. Next create the arrayadapter and position your objects.
  3. Create Activity declare the

    HttpClient client = new DefaultHttpClient();

                HttpGet request = new HttpGet("Url Json Url");
    
                HttpResponse response = client.execute(request);
    
                HttpEntity resEntity = response.getEntity();
    
                String json = EntityUtils.toString(resEntity);
    
                JSONArray jarray = new JSONArray(json);
    
                newsList = new ArrayList<NewsModel>();
    
                for (int i = 0; i < jarray.length(); i++)
    

    {

                    JSONObject row = jarray.getJSONObject(i);
    
                    NewsModel data = new NewsModel();
    
                    data.setTitle(row.getString("title"));
    
                    data.setContent(row.getString("content"));
    
                    data.setExcerpt(row.getString("excerpt"));
    
                    data.setDate(row.getString("date"));
    
                    data.setImage(row.getString("feature_image"));
    
                    newsList.add(data);
    
0

create model below that

ublic class NewsModel implements Serializable{

String title;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getExcerpt() {
    return excerpt;
}

public void setExcerpt(String excerpt) {
    this.excerpt = excerpt;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

String content;
String excerpt;
String date;
String image;

}