2013-04-16 39 views
1

enter image description here錯誤解析的ArrayList

 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)); 
        } 
       }; 
     } 

IM做一個列表視圖和我有這個問題跟它不匹配,但我不匹配,它也說,JSON數組的錯誤,我把代碼供參考,我不知道在哪裏以查找任何錯誤的想法?也說取錯,但我認爲它確定所以我的錯誤在哪裏呢?我回收了這段代碼,第一個活動很好地工作,這個活動不起作用,所以即時通訊想知道我在哪裏犯了一個錯誤..謝謝你

+0

客人不願意這樣寫:「today_details .setSunrise(row.getInt( 「小時」)+ row.getInt( 「分鐘」)); today_details.setSunset(row.getInt( 「小時」)+ row.getInt( 「分鐘」));「,使具有行JSONObject中有多個json對象,請參閱我的回答 –

+0

如果不添加更多數組json,那麼您的json類型不兼容JSONArray對象。它是兼容的JSONObject。 –

+0

發佈完整的json。 – rajeshwaran

回答

2

您的JSON返回數據爲JSONObjectJSONArray

更改代碼,

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

+0

行JSONArray jsonArray =新JSONArray(JSON)得到值時得到這個誤差 沒有包IDENTIFER;是我的錯誤?我應該把它改爲JSONObject jsonobj = new JSONObject(json);請更清楚地謝謝 – Giant

+0

然後JSONObject行= jsonArray.getJSONObject(i);部分獲取錯誤..getJSONObject這個人得到錯誤 – Giant

+0

請張貼您的完整的JSON。 – rajeshwaran

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")); 
+0

你剛剛在上面複製過嗎?相同的代碼 – Giant

+0

你並修改你的代碼。 –

0

我看到你的網址不數組JSON,讓您的固定代碼array.I的一個元素放碼的右側部分:

 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); 
+0

我的網址是json我用了一個就像它在另一個活動..我不需要放棄了嗎?和你的代碼有點....它是一個列表視圖隊友...幫助我,如果你可以其他代碼不工作大聲笑 – Giant

+0

如果你不把更多的數組JSON,你的JSON類型不兼容的JSONArray對象。它是兼容的JSONObject。 –

+0

你能爲我調試我的錯誤日誌嗎? – Giant