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ではないので、配列の1つの要素のコードを修正しました.コードの正しい部分を置きます:

        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 に答える