2012-05-02 7 views
1

私は単純な天気アプリを開発しています。私はGoogle天気APIを使用しています。 XMLファイルをダウンロードし、その情報に基づいてUIを構築しようとしています。AsyncTaskを使用してXMLファイルをダウンロードする

私はAsyncTaskを使用してXMLファイルをダウンロードして解析しますが、動作しません。コードを実行すると、エラーは発生しません。アプリケーションは起動しますが、xmlファイルの情報は表示されません。ここで

はコードです:

package com.android.weatherApp; 



public class WeatherAppActivity extends Activity { 

    static String url="http://www.google.com/ig/api?weather="; 
    static final String KEY_FORCAST_INFO="forcast_information"; 
    static final String KEY_CITY="city"; 
    static final String KEY_CURRENT_CONDITIONS="current_conditions"; 
    static final String KEY_CONDITION="condition"; 
    static final String KEY_TEMP="temp_c"; 
    static final String KEY_ICON_SRC="icon"; 
    static final String KEY_FORCAST_COND="forecast_conditions"; 
    static final String KEY_DAY_WEEK="day_of_week"; 
    static final String KEY_LOW_TEMP="low"; 
    static final String KEY_HIGH_TEMP="high"; 
    Document finalDoc; 
    XMLParser xmlParser; 
    private TextView currentWeather; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     downloadXMLFile task=new downloadXMLFile(); 
     task.execute("New+York"); 

    } 

    private class downloadXMLFile extends AsyncTask<String,Void,String> 
    { 
     @Override 
     protected String doInBackground(String... params) { 

      String xml=null; 

      try{ 
       DefaultHttpClient httpClient=new DefaultHttpClient(); 
       HttpPost httpPost=new HttpPost(url+params[0]); 
       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity=httpResponse.getEntity(); 
       xml=EntityUtils.toString(httpEntity); 

      } 
      catch(UnsupportedEncodingException e) 
      { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return xml; 
     } 

     @Override 
     protected void onPostExecute(String xmlString) 
     { 
      Document doc = null; 
      DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance(); 

      try { 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       InputSource is=new InputSource(); 

       is.setCharacterStream(new StringReader(xmlString)); 
       doc=db.parse(is); 

      } catch (ParserConfigurationException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SAXException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      finalDoc=doc; 

      NodeList nl=finalDoc.getElementsByTagName(KEY_CURRENT_CONDITIONS); 
       NodeList nl2=finalDoc.getElementsByTagName(KEY_FORCAST_COND); 
       currentWeather=(TextView)findViewById(R.id.currentState); 
       TextView currentTemp = (TextView)findViewById(R.id.tempC); 
       TextView currentLocation=(TextView)findViewById(R.id.currentLocation); 
       currentLocation.setText("Iasi"); 

       for(int i=0;i<nl.getLength();i++) 
       { 
       Element e= (Element)nl.item(i); 
       String cs=xmlParser.getValue(e, KEY_CONDITION).toUpperCase(); 
       currentWeather.setText(cs); 

       String temperature=xmlParser.getValue(e, KEY_TEMP); 
       currentTemp.setText(temperature); 

       String logoImg=xmlParser.getValue(e, KEY_ICON_SRC).toLowerCase(); 

       if(logoImg.equals("/ig/images/weather/sunny.gif")) 
       { 
        ImageView img=(ImageView)findViewById(R.id.wImg); 
        img.setImageResource(R.drawable.sun); 
       } 

       } 

       for(int i=0;i<nl2.getLength();i++) 
       { 
       Element e=(Element)nl2.item(i); 
       TextView day1=(TextView)findViewById(R.id.day1); 
       day1.setText("TOMORW"); 
       TextView day2=(TextView)findViewById(R.id.day2); 
       TextView day3=(TextView)findViewById(R.id.day3); 
       ImageView img1=(ImageView)findViewById(R.id.day1Logo); 
       ImageView img2=(ImageView)findViewById(R.id.day2Logo); 
       ImageView img3=(ImageView)findViewById(R.id.day3Logo); 
       TextView highTemp1=(TextView)findViewById(R.id.day1MaxTemp); 
       TextView highTemp2=(TextView)findViewById(R.id.day2MaxTemp); 
       TextView highTemp3=(TextView)findViewById(R.id.day3MaxTemp); 

       String d1=xmlParser.getValue(e, KEY_DAY_WEEK).toUpperCase(); 
       String t=xmlParser.getValue(e, KEY_HIGH_TEMP); //get the max value of temperature - Fahrenheit 
       int tempF=Integer.parseInt(t); // make the string a number 
       float tC=(float) ((5./9)*(tempF-32)); // calculate the Celsius value of temperature 
       tempF=(int)tC; // get only the Int value 
       t=Integer.toString(tempF); // transform the temperature value back to string 

       switch(i) 
       { 
       case 1: 
        highTemp1.setText(t); 
        break; 
       case 2: 
        highTemp2.setText(t); 
        day2.setText(d1); 
        break; 
       case 3: 
        highTemp3.setText(t); 
        day3.setText(d1); 
        break; 
       } 
       } 
     } 

    } 

してくださいあなたは私が間違って何をやっている私に伝えることができます。 ありがとう!

+0

あなたのアプリをデバッグしようとしましたか? –

+1

あなたはlogcatを投稿できますか?また、私はPostExecuteではなくDoInBackgroundでの解析を推奨します...しかし、個人的な好みです。 – Tony

答えて

2

最初に、url変数に「http://www.google.com/ig/api?weather=New+York」ではなく「http://www.google.com/ig/api?weather=」が含まれています。 XMLコンテンツは空です。

はdoInBackgroundメソッドでは、このような何かを試してみてください:

String currentUrl = url; 
if(params.length>0 && params[0]!=null)currentUrl+= params[0]; 

私はあなたがループ内であなたのビューをインスタンス化することを参照してください。 onCreateメソッドまたはonStartメソッドの中にそれらをインスタンス化する必要があります。これを行うと、アクティビティが作成された後にこれらの変数がインスタンス化され、パフォーマンスが得られます。

+0

"task.execute(" New + York ")を呼び出すと、httpPost = new HttpPost(url +" New York ")//実行された文字列 - したがってxmlファイルは空ではありません! – Alin

+0

他のアイデア? – Alin

+0

Areあなたのコードから、私は 'HttpPost httpPost = new HttpPost(url);'を参照しているので、あなたはあなたにパラメータ値を呼ぶことはありません。 –

0

マニフェストに<uses-permission android:name="android.permission.INTERNET"/>を追加しましたか?

Log.i("test",xmlString);をあなたのonPostExecuteに入れて、あなたのログに結果を見ることができますか?

+0

はい - マニフェストのパーミッションがあります!xmlStringはdoOnBackground()メソッドのXML文字列と同じです!私が使用しようとしているXMLファイルから – Alin

+0

あなたのビューのインスタンシエーションをonStartメソッドに入れてみることはできますか? –

関連する問題