English 中文(简体)
Google 飞艇和机器人
原标题:google api android
  • 时间:2012-05-23 02:48:28
  •  标签:
  • android

我前往法国的云层显示天气信息, 但我不能, 这个代码与这个基础URL < a href=" http://www.google.com/ig/api?weather=city, state" rel="nofollow" > http://www.google.com/ig/api?weather=city, state 一起工作, 但如果改用法语,这行不通, 并且是一个例外!

public class WeatherXMLParsing extends GDActivity{
static final String baseURL="http://www.google.com/ig/api?weather=city,state&hl=fr";    
EditText city,state;
TextView tv1,tv2,tv3,tv4,tv5;
ImageView iv;
String c,s;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setActionBarContentView(R.layout.weather_layout_base);

    tv1=(TextView) findViewById(R.id.weath_info);
    tv2=(TextView) findViewById(R.id.weath_temp_x);
    tv3=(TextView) findViewById(R.id.weath_wind_x);
    tv4=(TextView) findViewById(R.id.weath_humidity_x);
    tv5=(TextView) findViewById(R.id.weath_condition_x);
    iv=(ImageView) findViewById(R.id.weath_img);

    DataBind();
}

public void DataBind() {

    StringBuilder URL=new StringBuilder(baseURL);
    String FullURL=URL.toString();
    try {

        URL Website = new URL(FullURL);
        SAXParserFactory spf=SAXParserFactory.newInstance(); 
        SAXParser sp=spf.newSAXParser();
        XMLReader xr=sp.getXMLReader();
        HandlingXMLStuff doingWork=new HandlingXMLStuff();
        xr.setContentHandler(doingWork);
        xr.parse(new InputSource(Website.openStream()));

        String information=doingWork.getInformation();
        String temp=doingWork.getTempInformation();
        String wind=doingWork.getWindInformation();
        String humudity=doingWork.getHumudityInformation();
        String condition=doingWork.getConditionInformation();

        Log.d("weath_info", information);
        Log.d("weath_info", temp);
        Log.d("weath_temp", wind);
        Log.d("weath_wind", humudity);
        Log.d("weath_humud", condition);

        tv1.setText(information);
        tv2.setText(temp+"°C");
        tv3.setText(wind);
        tv4.setText(humudity);
        tv5.setText(condition);

                    if(condition.equals("Cloudy"))
                    {
                        iv.setImageResource(R.drawable.cloudy);
                    }
                    else if(condition.equals("Partly Cloudy"))
                    {
                        iv.setImageResource(R.drawable.partly_cloudy);
                    }
                    else if(condition.equals("Rainy"))
                    {
                        iv.setImageResource(R.drawable.rainy);
                    }
                    else if(condition.equals("Clear"))
                    {
                        iv.setImageResource(R.drawable.sunny);
                    }
                    else{
                        iv.setImageResource(R.drawable.mostly_sunny);
                    } 
    } catch (Exception e) {
tv1.setText("Erreur internet");
tv2.setVisibility(View.INVISIBLE);
tv3.setVisibility(View.INVISIBLE);


    }
}
}





public class HandlingXMLStuff extends DefaultHandler {
XMLDataCollected info=new XMLDataCollected();
public String getTempInformation(){

    return info.tempToString();
}
public String getConditionInformation(){

    return info.conditionToString();
}
public String getWindInformation(){

    return info.windToString();
}
public String getHumudityInformation(){

    return info.humudityToString();
}
public String getInformation(){

    return info.informationToString();
}

@Override
public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {

    if (localName.equals("city"))
    {
        String city=attributes.getValue("data");    
        info.setCity(city);
    }

    else if (localName.equals("temp_c"))
    {
        String t=attributes.getValue("data");
        int temp=Integer.parseInt(t);
        info.setTemp(temp);
    }

    else if (localName.equals("humidity"))
    {
        String humidity=attributes.getValue("data");
        info.setHumidity(humidity);
    }

    else if (localName.equals("wind_condition"))
    {
        String wind=attributes.getValue("data");
        info.setWind(wind);
    }

    else if (localName.equals("forecast_date"))
    {
        String date=attributes.getValue("data");
        info.setDate(date);
    }
    else if (localName.equals("condition"))
    {
        String condition=attributes.getValue("data");
        info.setCondition(condition);
    }
}



}










public class XMLDataCollected {

String city=null,humidity=null,wind=null,date=null,condition=null;
int temp=0;

public void setCity(String c)
{
    city=c; }
public void setCondition(String cnd)
{
    condition=cnd;  }
public void setTemp(int t)
{
    temp=t; }
public void setHumidity(String h)
{
    humidity=h; }
public void setWind(String w)
{
    wind=w; }
public void setDate(String d)
{
    date=d; }
public String cityToString()
{
    return city;    
}

public String conditionToString()
{
    return condition;   
}
public String informationToString()
{
    return "Weather in "+city+" for "+date; 
}
public String tempToString()
{
    return temp+""; 
}
public String humudityToString()
{
    return humidity;    
}
public String windToString()
{
    return wind;    
}
}

这是堆栈跟踪 :

05-23 03:20:04.376: W/System.err(6355): java.net.UnknownHostException: Host is unresolved: www.google.com:80
05-23 03:20:04.386: W/System.err(6355):     at java.net.Socket.connect(Socket.java:1037)
05-23 03:20:04.396: W/System.err(6355):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
05-23 03:20:04.396: W/System.err(6355):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:145)
05-23 03:20:04.406: W/System.err(6355):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:67)
05-23 03:20:04.406: W/System.err(6355):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:821)
05-23 03:20:04.416: W/System.err(6355):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:807)
05-23 03:20:04.416: W/System.err(6355):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1051)
05-23 03:20:04.416: W/System.err(6355):     at java.net.URL.openStream(URL.java:653)
05-23 03:20:04.436: W/System.err(6355):     at com.pfe.meteo.WeatherXMLParsing.DataBind(WeatherXMLParsing.java:55)
05-23 03:20:04.446: W/System.err(6355):     at com.pfe.meteo.WeatherXMLParsing.onCreate(WeatherXMLParsing.java:40)
05-23 03:20:04.446: W/System.err(6355):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 03:20:04.456: W/System.err(6355):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-23 03:20:04.456: W/System.err(6355):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-23 03:20:04.466: W/System.err(6355):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-23 03:20:04.466: W/System.err(6355):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-23 03:20:04.466: W/System.err(6355):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 03:20:04.476: W/System.err(6355):     at android.os.Looper.loop(Looper.java:123)
05-23 03:20:04.476: W/System.err(6355):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-23 03:20:04.488: W/System.err(6355):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 03:20:04.488: W/System.err(6355):     at java.lang.reflect.Method.invoke(Method.java:521)
05-23 03:20:04.488: W/System.err(6355):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-23 03:20:04.496: W/System.err(6355):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-23 03:20:04.496: W/System.err(6355):     at dalvik.system.NativeStart.main(Native Method)
问题回答

检查您的互联网连接和 DNS 服务器 - 似乎有问题





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签