1

在此处输入图像描述

        package com.bpi.gears;

    public class TodayDetails {

        private String location;
        private String weather;
        private int temp_c;
        private String relative_humidity;
        private String wind_dir;
        private int wind_kph;
        private int wind_gust_kph;
        private String pressure_mb;
        private int dewpoint_c;
        private String heat_index_c;
        private String visibility_km;
        private String precipitation;
        private String daily_precipitation;
        private String sunrise;
        private String sunset;
        private String icon_url;

        public String getLocation() {
            return location;
        }
        public void setLocation(String location) {
            this.location = location;
        }
        public String getWeather() {
            return weather;
        }
        public void setWeather(String weather) {
            this.weather = weather;
        }
            public int getTemp_c() {
            return temp_c;
        }
        public void setTemp_c(int temp_c) {
            this.temp_c = temp_c;
        }

        public String getRelative_humidity() {
            return relative_humidity;
        }
        public void setRelative_humidity(String relative_humidity) {
            this.relative_humidity = relative_humidity;
        }
        public String getWind_dir() {
            return wind_dir;
        }
        public void setWind_dir(String wind_dir) {
            this.wind_dir = wind_dir;
        }
        public int getWind_kph() {
            return wind_kph;
        }
        public void setWind_kph(int wind_kph) {
            this.wind_kph = wind_kph;
        }
        public int getWind_gust_kph() {
            return wind_gust_kph;
        }
        public void setWind_gust_kph(int wind_gust_kph) {
            this.wind_gust_kph = wind_gust_kph;
        }
        public String getPressure_mb() {
            return pressure_mb;
        }
        public void setPressure_mb(String pressure_mb) {
            this.pressure_mb = pressure_mb;
        }
        public int getDewpoint_c() {
            return dewpoint_c;
        }
        public void setDewpoint_c(int dewpoint_c) {
            this.dewpoint_c = dewpoint_c;
        }
        public String getHeat_index_c() {
            return heat_index_c;
        }
        public void setHeat_index_c(String heat_index_c) {
            this.heat_index_c = heat_index_c;
        }
        public String getVisibility_km() {
            return visibility_km;
        }
        public void setVisibility_km(String visibility_km) {
            this.visibility_km = visibility_km;
        }
        public String getPrecipitation() {
            return precipitation;
        }
        public void setPrecipitation(String precipitation) {
            this.precipitation = precipitation;
        }
        public String getDaily_precipitation() {
            return daily_precipitation;
        }
        public void setDaily_precipitation(String daily_precipitation) {
            this.daily_precipitation = daily_precipitation;
        }
        public String getSunrise() {
            return sunrise;
        }
        public void setSunrise(String sunrise) {
            this.sunrise = sunrise;
        }
        public String getSunset() {
            return sunset;
        }
        public void setSunset(String sunset) {
            this.sunset = sunset;
        }
        public String getIcon_url() {
            return icon_url;
        }
        public void setIcon_url(String icon_url) {
            this.icon_url = icon_url;
        }



    }
this is what your saying that is missing?this is my detail file i hope you can find my problem.this activity saves all strings and integer files in my activity.its all here so i know i didnt miss a thing

    package com.bpi.gears;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class WeatherToday extends Activity {
    ListView list_view_main;
    ProgressDialog pd=null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weathertoday);
        this.pd = ProgressDialog.show(this, "Weather Today", "Please wait while fetching data...");
        GetAllToday();
    }
    private void GetAllToday() {
        (new fetchTodaysWeather(this)).execute();
    }
    private class fetchTodaysWeather extends AsyncTask<Void, ArrayList<TodayDetails>, ArrayList<TodayDetails>> {
        @SuppressWarnings("unused")
        Context mContext;
        public fetchTodaysWeather(Context context) {
            super();
            mContext = context;
         }
        ArrayList<TodayDetails> search_results = new ArrayList<TodayDetails>();
        TodayDetails today_details;
        @Override
        protected ArrayList<TodayDetails> doInBackground(Void... params) {

                        // Execute the request in the client
                        HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
                        // Grab the response
                        BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
                        String json = reader.readLine();
                        JSONArray jsonArray = new JSONArray(json);
                        for(int i = 0; i < jsonArray.length(); i++) {
                            JSONObject row = jsonArray.getJSONObject(i);
                            today_details = new TodayDetails();
                            today_details.setLocation(row.getString("location"));
                            today_details.setWeather(row.getString("weather"));
                            today_details.setTemp_c(row.getInt("temp_c"));
                            today_details.setRelative_humidity(row.getString("relative_humidity"));
                            today_details.setWind_dir(row.getString("wind_dir"));
                            today_details.setWind_kph(row.getInt("wind_kph"));
                            today_details.setWind_gust_kph(row.getInt("wind_gust_kph"));
                            today_details.setWind_gust_kph(row.getInt("wind_gust_kph"));
                            today_details.setPressure_mb(row.getString("pressure_mb"));
                            today_details.setDewpoint_c(row.getInt("dewpoint_c"));
                            today_details.setHeat_index_c(row.getString("heat_index_c"));
                            today_details.setVisibility_km(row.getString("visibility_km"));
                            today_details.setPrecipitation(row.getString("lrecipitation"));
                            today_details.setDaily_precipitation(row.getString("daily_precipitation"));
                            today_details.setSunrise(row.getInt("hour")+row.getInt("minutes"));
                            today_details.setSunset(row.getInt("hour")+row.getInt("minutes"));
                            search_results.add(today_details);
                        }
                    } catch(Exception e){
                       e.printStackTrace();
                    }
                    return search_results;
                }
                protected void onPostExecute(ArrayList<TodayDetails> search_results) {
                    if(WeatherToday.this.pd != null){
                        WeatherToday.this.pd.dismiss();
                    }
                    ArrayList<TodayDetails> today_details = search_results;
                    list_view_main = (ListView)findViewById(R.id.todaylist);
                    list_view_main.setAdapter(new TodayListAdapter(getApplicationContext(), today_details));
                    }
                };
        }

我正在制作一个列表视图,我遇到了这个问题,它说不匹配,但我没有不匹配,它还说 json 数组错误认为没关系,那么我的错误在哪里?我回收了这个代码,第一个活动工作正常这个活动不工作所以我想知道我在哪里犯了错误..谢谢

4

3 回答 3

2

你的 json 返回数据JSONObject不是 JSONArray

更改您的代码,

JSONArray jsonArray = new JSONArray(json);

更改JSONArrayJSONObject,

 JSONObject jsonobj = new JSONObject(json);


                today_details = new TodayDetails();
                today_details.setLocation(jsonobj.getString("location"));
                today_details.setWeather(jsonobj.getString("weather"));
                today_details.setTemp_c(jsonobj.getInt("temp_c"));
                today_details.setRelative_humidity(jsonobj.getString("relative_humidity"));
                today_details.setWind_dir(jsonobj.getString("wind_dir"));
                today_details.setWind_kph(jsonobj.getInt("wind_kph"));
                today_details.setWind_gust_kph(jsonobj.getInt("wind_gust_kph"));
                today_details.setWind_gust_kph(jsonobj.getInt("wind_gust_kph"));
                today_details.setPressure_mb(jsonobj.getString("pressure_mb"));
                today_details.setDewpoint_c(jsonobj.getInt("dewpoint_c"));
                today_details.setHeat_index_c(jsonobj.getString("heat_index_c"));
                today_details.setVisibility_km(jsonobj.getString("visibility_km"));
                today_details.setPrecipitation(jsonobj.getString("lrecipitation"));
                today_details.setDaily_precipitation(jsonobj.getString("daily_precipitation"));
            JSONObject  sunrise = jsonobj.getJSONObject("sunrise");
    today_details.setSunrise(String.valueOf(sunrise.getInt("hour"))+String.valueOf(sunrise.getInt("minutes")));
            JSONObject  sunset = jsonobj.getJSONObject("sunset");
                   today_details.setSunset(String.valueOf(sunset.getInt("hour"))+String.valueOf(sunset.getInt("minutes")));                    
                search_results.add(today_details);

更新

对于参考, JSONObject由 覆盖{}JSONArray覆盖[]

于 2013-04-16T06:44:13.543 回答
0
     use this way...

        JSONObject row = new JSONObject (json);

       today_details.setLocation(row.getString("location"));
       today_details.setWeather(row.getString("weather"));
       today_details.setTemp_c(row.getInt("temp_c"));
       today_details.setRelative_humidity(row.getString("relative_humidity"));
       today_details.setWind_dir(row.getString("wind_dir"));
         today_details.setWind_kph(row.getInt("wind_kph"));
于 2013-04-16T06:53:32.967 回答
0

我看到您的 url 不是数组 json,因此将您的代码固定为数组的一个元素。我放置了代码的正确部分:

        JSONObject row = new JSONObject(json);
        String s = row.getString("location");

        JSONObject inlineRow = row.getJSONObject("sunrise");
        int t1 = inlineRow.getInt("hour") + inlineRow.getInt("minute");
        Log.i("Location:",s + " Sunrise[hour:" + inlineRow.getInt("hour") + ", minute:" + inlineRow.getInt("minute") + "] = " + t1);
于 2013-04-16T07:07:38.843 回答