2016-03-16 33 views
0
} else if (xpp.getName().equalsIgnoreCase("pressure")) { 
     if (insideItem) { 
      Pressure.add(xpp.nextText()); 
     } 
} else if (xpp.getName().equalsIgnoreCase("weatherIconUrl")) { 
     if (insideItem) { 
      iconImg.add(xpp.nextText()); 
     } 

我想從天氣api中調用天氣項目。我試圖撥打的所有文本項都可以,但iconImg不是。 iconImg.add(xpp.nextText())給出錯誤。我可能不應該在這裏使用imageview,但我不知道還有什麼要做。任何幫助將不勝感激。如何使用xmlPullParser從天氣api調用圖像?

這裏是整個代碼:

public class HavaDurumu2 extends FragmentActivity { 

private String urll; 
public TextView xmlShow; 
private EditText lattitudeEditText; 
private EditText longitudeEditText; 
private Button devam; 
public Intent dataShowIntent; 
public TextView succcess; 
public TextView appTitle; 
public TextView lat; 
public TextView lon; 
GoogleMap googlelMap; 
Marker mk; 
Button fav; 
TextView error; 
Intent vj; 
public static List<String> Time; 
public static List<String> CTemperature; 
public static List<String> Wind_Speed_Kmph; 
public static List<String> Water_Temperature_C; 
public static List<String> Humidity; 
public static List<String> Significant_Wave_Height; 
public static List<String> Swell_Height; 
public static List<String> Swell_Direction; 
public static List<String> Swell_Period; 
public static List<String> Pressure; 
public static List<String> Visibility; 
public static List<String> Weather_Condition; 
public static List<String> Warning_level; 
public static List<Double> Wind_Speed_Kmph_Num; 
public static List<Double> Wind_Speed_mps_Num; 
public static List<String> Wind_Dir_Degree; 
public static List<Double> Beaufort; 
public static List<String> nearest_longitude; 
public static List<String> nearest_latitude; 
public static List<String> nearest_distance_mile; 
public static List<String> date; 
public static List<String> maxtempc; 
public static List<String> mintempc; 
public static List<String> nearest_location_name; 
public static List<String> sunrise; 
public static List<String> sunset; 
public static List<ImageView> iconImg; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_havadurumu2); 




    if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) { 
     SupportMapFragment mapfragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     googlelMap = mapfragment.getMap(); 
     lattitudeEditText = (EditText) findViewById(R.id.enEditText); 
     longitudeEditText = (EditText) findViewById(R.id.boyEditText); 
     lat = (TextView) findViewById(R.id.enlem); 
     lon = (TextView) findViewById(R.id.boylam); 
     devam = (Button) findViewById(R.id.devam); 

     googlelMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39, 30), 5.0f)); 


     lattitudeEditText.clearFocus(); 


     longitudeEditText.clearFocus(); 




     Time = new ArrayList<String>(); 
     CTemperature = new ArrayList<String>(); 
     Wind_Speed_Kmph = new ArrayList<String>(); 
     Water_Temperature_C = new ArrayList<String>(); 
     Humidity = new ArrayList<String>(); 

     Significant_Wave_Height = new ArrayList<String>(); 
     Swell_Height = new ArrayList<String>(); 
     Swell_Direction = new ArrayList<String>(); 
     Swell_Period = new ArrayList<String>(); 
     Pressure = new ArrayList<String>(); 
     Visibility = new ArrayList<String>(); 
     Warning_level = new ArrayList<String>(); 
     Wind_Dir_Degree = new ArrayList<String>(); 
     sunrise = new ArrayList<String>(); 
     sunset = new ArrayList<String>(); 
     Wind_Speed_Kmph_Num = new ArrayList<Double>(); 
     Wind_Speed_mps_Num = new ArrayList<Double>(); 
     Beaufort = new ArrayList<Double>(); 
     Weather_Condition = new ArrayList<String>(); 
     iconImg =new ArrayList<ImageView>(); 

     nearest_longitude = new ArrayList<String>(); 
     nearest_latitude = new ArrayList<String>(); 
     nearest_distance_mile = new ArrayList<String>(); 
     date = new ArrayList<String>(); 
     maxtempc = new ArrayList<String>(); 
     mintempc = new ArrayList<String>(); 
     nearest_location_name = new ArrayList<String>(); 




     String message = "Yer secmek için ekrana uzun basın"; 


     Toast t = Toast.makeText(this, message, Toast.LENGTH_LONG); 
     t.show(); 
     googlelMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() { 


      @Override 
      public void onMapLongClick(LatLng point) { 
       if (mk == null) { 


        mk = googlelMap.addMarker(new MarkerOptions().position(point).snippet("Enlem : " + point.latitude + "Boylam : " + point.longitude).title("Kordinatlar")); 
        lattitudeEditText.setText(("" + point.latitude).substring(0, 9)); 
        longitudeEditText.setText(("" + point.longitude).substring(0, 9)); 
        // fav.setVisibility(View.VISIBLE); 
       } else { 


        mk.remove(); 
        mk = googlelMap.addMarker(new MarkerOptions().position(point).snippet("Enlem : " + point.latitude + "Boylam : " + point.longitude).title("Kordinatlar")); 
        lattitudeEditText.setText("" + point.latitude); 
        longitudeEditText.setText("" + point.longitude); 


       } 


      } 
     }); 


    } else { 
     String message = "Google Play servisi uygun değil"; 

     Toast.makeText(HavaDurumu2.this, message, Toast.LENGTH_LONG).show(); 


     //GooglePlayServicesUtil.getErrorDialog(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this),this,1001).show(); 
    } 
} 




public void clickHandler(View v) { 




    if (lattitudeEditText.getText().toString().isEmpty() && longitudeEditText.getText().toString().isEmpty()) { 
     String message = "Lütfen Bir Yer Seçin"; 
     Toast.makeText(HavaDurumu2.this, "Lütfen Bir Yer Seçin", Toast.LENGTH_SHORT).show(); 
    } else if (networkStatus(HavaDurumu2.this)) { 
     urll = "http://api.worldweatheronline.com/free/v2/marine.ashx?q=" + lattitudeEditText.getText().toString() + "%2C" + longitudeEditText.getText().toString() + "&tp=3&lang=tr&format=xml&key=......"; 
     new LoadAssync().execute(); 


    } else { 
     String message = "İnternet Bağlantısı Bulunamdı"; 
     Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT); 
     toast.setGravity(0, 0, 300); 
     toast.show(); 


    } 


} 


private class LoadAssync extends AsyncTask<String, Void, Void> { 


    ProgressDialog dialog = new ProgressDialog(HavaDurumu2.this); 


    protected void onPreExecute() { 
     dialog.setMessage("Bilgiler Alınıyor..."); 
     dialog.show(); 
    } 


    protected Void doInBackground(final String... args) { 


     try { 
      getdata(); 


      updateDateandTemperature(); 
      updatedirection(); 
      updateAstronomy(); 
      updateweather(); 
      Log.i("", String.valueOf(date.size()) + " " + String.valueOf(Wind_Dir_Degree.size()) + " " + String.valueOf(sunrise.size())); 




     } catch (Exception ex) { 
      String message = "Hata oluştu"; 
      Toast.makeText(HavaDurumu2.this, message, Toast.LENGTH_SHORT).show(); 
     } 
     return null; 


    } 


    protected void onPostExecute(final Void unused) { 


     if (dialog.isShowing()) { 
      dialog.dismiss(); 


      if (Time.size() == 0) { 
       nearest_distance_mile.clear(); 
       nearest_latitude.clear(); 
       nearest_location_name.clear(); 
       nearest_longitude.clear(); 
       maxtempc.clear(); 
       mintempc.clear(); 
       date.clear(); 
       sunrise.clear(); 
       sunset.clear(); 




       String message = "Bu bölge için durum bilgisi yok"; 
       Toast.makeText(HavaDurumu2.this, message, Toast.LENGTH_SHORT).show(); 
      } else { 

        Intent m = new Intent(HavaDurumu2.this, ShowWeather.class); 

        startActivity(m); 
       } 
      } 

     } 

    } 

    private void updatefeeds() { 
     try { 
      Time.clear(); 
      CTemperature.clear(); 
      Wind_Speed_Kmph.clear(); 
      Wind_Speed_Kmph_Num = new ArrayList<Double>(); 
      Water_Temperature_C.clear(); 
      Humidity.clear(); 
      ; 
      Significant_Wave_Height.clear(); 
      Swell_Height.clear(); 
      Swell_Direction.clear(); 
      Swell_Period.clear(); 
      Pressure.clear(); 
      Visibility.clear(); 
      Warning_level.clear(); 
      Beaufort.clear(); 
      Wind_Dir_Degree.clear(); 
      Weather_Condition.clear(); 
      iconImg.clear(); 

      URL url = new URL(urll); 


      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      factory.setNamespaceAware(false); 
      XmlPullParser xpp = factory.newPullParser(); 

      xpp.setInput(getInputStream(url), "UTF_8"); 


      boolean insideItem = false; 


      int eventType = xpp.getEventType(); 


      while (eventType != XmlPullParser.END_DOCUMENT) { 


       if (eventType == XmlPullParser.START_TAG) { 


        if (xpp.getName().equalsIgnoreCase("hourly")) { 


         insideItem = true; 

        } 

        else if (xpp.getName().equalsIgnoreCase("time")) { 

         if (insideItem) { 
          Time.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("tempc")) { 

         if (insideItem) { 
          CTemperature.add(xpp.nextText()); 

         } 

        } else if (xpp.getName().equalsIgnoreCase("windspeedKmph")) { 


         if (insideItem) { 
          Wind_Speed_Kmph.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("winddirdegree")) { 

         if (insideItem) { 
          Wind_Dir_Degree.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("waterTemp_C")) { 

         if (insideItem) { 
          Water_Temperature_C.add(xpp.nextText()); 

         } 

        } else if (xpp.getName().equalsIgnoreCase("humidity")) { 

         if (insideItem) { 
          Humidity.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("sigHeight_m")) { 

         if (insideItem) { 
          Significant_Wave_Height.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("swellHeight_m")) { 

         if (insideItem) { 
          Swell_Height.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("swellDir")) { 

         if (insideItem) { 
          Swell_Direction.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("swellPeriod_secs")) { 

         if (insideItem) { 
          Swell_Period.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("visibility")) { 

         if (insideItem) { 
          Visibility.add(xpp.nextText()); 

         } 
        } else if (xpp.getName().equalsIgnoreCase("pressure")) { 

         if (insideItem) { 
          Pressure.add(xpp.nextText()); 

         } 

        } else if (xpp.getName().equalsIgnoreCase("weatherIconUrl")) { 

         if (insideItem) { 
          iconImg.add(xpp.nextText()); 

         } 

        } else if (xpp.getName().equalsIgnoreCase("lang_tr")) { 

         if (insideItem) { 
          Weather_Condition.add(xpp.nextText()); 
          Log.i("", Weather_Condition.get(Weather_Condition.size() - 1)); 

         } 
        } 


       } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("hourly")) { 


        insideItem = false; 
       } 

       eventType = xpp.next(); 

      } 

     } catch (Exception e) { 


      Log.i("", e.toString()); 
     } 


    } 


    public InputStream getInputStream(URL url) { 
     try { 
      return url.openConnection().getInputStream(); 
     } catch (IOException e) { 
      return null; 
     } 
    } 


    public void updateDateandTemperature() { 
     try { 


      URL url = new URL(urll); 


      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      factory.setNamespaceAware(false); 
      XmlPullParser xpp = factory.newPullParser(); 


      date.clear(); 
      maxtempc.clear(); 
      mintempc.clear(); 


      xpp.setInput(getInputStream(url), "UTF_8"); 


      boolean insideItem = false; 

      int eventType = xpp.getEventType(); 


      while (eventType != XmlPullParser.END_DOCUMENT) { 


       if (eventType == XmlPullParser.START_TAG) { 


        if (xpp.getName().equalsIgnoreCase("weather")) { 


         insideItem = true; 


        } else if (xpp.getName().equalsIgnoreCase("date")) { 


         if (insideItem) { 
          date.add(xpp.nextText()); 
         } 
        } else if (xpp.getName().equalsIgnoreCase("maxtempc")) { 


         if (insideItem) { 
          maxtempc.add(xpp.nextText()); 
         } 
        } else if (xpp.getName().equalsIgnoreCase("mintempc")) { 


         if (insideItem) { 
          mintempc.add(xpp.nextText()); 
         } 
        } 


       } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("weather")) { 


        insideItem = false; 
       } 


       eventType = xpp.next(); 

      } 


     } catch (Exception e) { 

     } 
    } 


    public void updateAstronomy() { 
     try { 


      URL url = new URL(urll); 


      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      factory.setNamespaceAware(false); 
      XmlPullParser xpp = factory.newPullParser(); 


      sunrise.clear(); 
      sunset.clear(); 


      xpp.setInput(getInputStream(url), "UTF_8"); 


      boolean insideItem = false; 


      int eventType = xpp.getEventType(); 


      while (eventType != XmlPullParser.END_DOCUMENT) { 


       if (eventType == XmlPullParser.START_TAG) { 


        if (xpp.getName().equalsIgnoreCase("astronomy")) { 


         insideItem = true; 


        } else if (xpp.getName().equalsIgnoreCase("sunrise")) { 


         if (insideItem) { 
          sunrise.add(xpp.nextText()); 


         } 
        } else if (xpp.getName().equalsIgnoreCase("sunset")) { 


         if (insideItem) { 
          sunset.add(xpp.nextText()); 
         } 
        } 


       } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("weather")) { 


        insideItem = false; 
       } 

       eventType = xpp.next(); 
      } 

     } catch (Exception e) { 

     } 
    } 


    public void getdata() { 
     updatefeeds(); 


     for (int i = 0; i <= Wind_Speed_Kmph.size() - 1; i++) 
      Wind_Speed_Kmph_Num.add(Double.parseDouble(Wind_Speed_Kmph.get(i))); 


     for (int i = 0; i <= Wind_Speed_Kmph.size() - 1; i++) 
      Wind_Speed_mps_Num.add(Wind_Speed_Kmph_Num.get(i) * 1000.0/3600.0); 


     for (int i = 0; i <= Wind_Speed_mps_Num.size() - 1; i++) 
      Beaufort.add(Math.cbrt((Wind_Speed_mps_Num.get(i)/0.837) * (Wind_Speed_mps_Num.get(i)/0.837))); 


     updatewarninglevels(); 
    } 

    public void updatedirection() { 
     for (int i = 0; i < Swell_Direction.size(); i++) { 
      if (Double.parseDouble(Swell_Direction.get(i)) >= 337.5 || Double.parseDouble(Swell_Direction.get(i)) <= 22.5) 
       Swell_Direction.set(i, Swell_Direction.get(i) + "°K"); 
      else if (Double.parseDouble(Swell_Direction.get(i)) <= 67.5) 
       Swell_Direction.set(i, Swell_Direction.get(i) + "°KD"); 
      else if (Double.parseDouble(Swell_Direction.get(i)) <= 112.5) 
       Swell_Direction.set(i, Swell_Direction.get(i) + "°D"); 
      else if (Double.parseDouble(Swell_Direction.get(i)) <= 157.5) 
       Swell_Direction.set(i, Swell_Direction.get(i) + "°GD"); 
      else if (Double.parseDouble(Swell_Direction.get(i)) <= 202.5) 
       Swell_Direction.set(i, Swell_Direction.get(i) + "°G"); 
      else if (Double.parseDouble(Swell_Direction.get(i)) <= 247.5) 
       Swell_Direction.set(i, Swell_Direction.get(i) + "°GB"); 
      else if (Double.parseDouble(Swell_Direction.get(i)) <= 292.5) 
       Swell_Direction.set(i, Swell_Direction.get(i) + "°B"); 
      else if (Double.parseDouble(Swell_Direction.get(i)) <= 337.5) 
       Swell_Direction.set(i, Swell_Direction.get(i) + "°KB"); 
     } 


     for (int i = 0; i < Wind_Dir_Degree.size(); i++) { 
      if (Double.parseDouble(Wind_Dir_Degree.get(i)) >= 337.5 || Double.parseDouble(Wind_Dir_Degree.get(i)) <= 22.5) 
       Wind_Dir_Degree.set(i, Wind_Dir_Degree.get(i) + "°K"); 
      else if (Double.parseDouble(Wind_Dir_Degree.get(i)) <= 67.5) 
       Wind_Dir_Degree.set(i, Wind_Dir_Degree.get(i) + "°KD"); 
      else if (Double.parseDouble(Wind_Dir_Degree.get(i)) <= 112.5) 
       Wind_Dir_Degree.set(i, Wind_Dir_Degree.get(i) + "°D"); 
      else if (Double.parseDouble(Wind_Dir_Degree.get(i)) <= 157.5) 
       Wind_Dir_Degree.set(i, Wind_Dir_Degree.get(i) + "°GD"); 
      else if (Double.parseDouble(Wind_Dir_Degree.get(i)) <= 202.5) 
       Wind_Dir_Degree.set(i, Wind_Dir_Degree.get(i) + "°G"); 
      else if (Double.parseDouble(Wind_Dir_Degree.get(i)) <= 247.5) 
       Wind_Dir_Degree.set(i, Wind_Dir_Degree.get(i) + "°GB"); 
      else if (Double.parseDouble(Wind_Dir_Degree.get(i)) <= 292.5) 
       Wind_Dir_Degree.set(i, Wind_Dir_Degree.get(i) + "°B"); 
      else if (Double.parseDouble(Wind_Dir_Degree.get(i)) <= 337.5) 
       Wind_Dir_Degree.set(i, Wind_Dir_Degree.get(i) + "°KB"); 
     } 

    } 


    public void updatewarninglevels() { 
     for (int i = 0; i <= Beaufort.size() - 1; i++) { 

       if (Beaufort.get(i) > 0 && Beaufort.get(i) <= 3.5) 
        Warning_level.add("Sakin 0 - 3.5 Bofor Skalası"); 
       else if (Beaufort.get(i) <= 5.6) 
        Warning_level.add("Rüzgarlı 3.5- 5.5 Bofor Skalası"); 
       else if (Beaufort.get(i) <= 9.5) 
        Warning_level.add("Fırtına 5.6- 9.5 Bofor Skalası"); 
       else 
        Warning_level.add("Kasırga 9.5 - üstü Bofor Skalası"); 
     } 
    } 


    public void updateweather() { 


     for (int i = 0; i <= Weather_Condition.size() - 1; i++) { 


     } 
    } 

什麼想法?

回答

0

改爲使用字符串

String artistPic = null;代替imageview的

得到像

artistPic = readArtistPic(解析器)的值;

+0

謝謝@Kumar。我是Android新手。你能否請清楚。我從另一個活動中調用圖像。 –