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