Baidu Weather API Usage test

Source: Internet
Author: User

The request function is a template for Baidu, and the URL and key are correct, but the return value is nothing. It's been bothering me all day.

1  Public classMainactivityextendsActivity {2     3 Button GetWeather;4 TextView Showjson;5 6String Httpurl = "Http://apis.baidu.com/apistore/weatherservice/weather";7String Httparg = "Citypinyin=beijing";8String Jsonresult =request (Httpurl, httparg);9 Ten @Override One     protected voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -  theGetWeather =(Button) Findviewbyid (r.id.getweather); -Buttonlistener listener =NewButtonlistener (); - Getweather.setonclicklistener (listener); -Showjson =(TextView) Findviewbyid (R.id.showjson); +          -     } +  A     classButtonlistenerImplementsOnclicklistener { at  - @Override -          Public voidOnClick (View v) { -             //TODO auto-generated Method Stub -             Switch(V.getid ()) { -              CaseR.id.getweather: in //Toast.maketext (Mainactivity.this, jsonresult.tostring (), Toast.length_short). Show (); - //if (Request (Httpurl, httparg) = = "") to //log.d ("mainactivity", "Asdasdasdad"); + //Else LOG.D ("mainactivity", Request (Httpurl, Httparg)); - Showjson.settext (Request (Httpurl, httparg));  the                  Break; *             default: $                  Break;Panax Notoginseng         } -         } the          +     } A  the      Public Staticstring Request (String Httpurl, String httparg) { +BufferedReader reader =NULL; -String result =NULL; $StringBuffer SBF =NewStringBuffer (); $Httpurl = Httpurl + "?" +Httparg; -  -         Try { theURL url =NewURL (httpurl); -HttpURLConnection connection =(httpurlconnection) URLWuyi . OpenConnection (); theConnection.setrequestmethod ("GET"); -             //fill in Apikey to HTTP header WuConnection.setrequestproperty ("Apikey", "c71ca21053ffc590e06f324968896823"); - Connection.connect (); AboutInputStream is =Connection.getinputstream (); $Reader =NewBufferedReader (NewInputStreamReader (IS, "UTF-8")); -String Strread =NULL; -              while((Strread = Reader.readline ())! =NULL) { - sbf.append (strread); ASbf.append ("\ r \ n"); +             } the reader.close (); -result =sbf.tostring (); $}Catch(Exception e) { the e.printstacktrace (); the         } the         returnresult; the     } -  in  the}

Baidu Weather API Usage test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.