我是Android的新手并开始了我的第一个API访问google wheather api,但我无法弄清楚为什么它没有向我显示任何值。我的代码是这样的:
package com.mubu.wheathertoday.wheathertoday;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;
import javax.net.ssl.HttpsURLConnection;
public class WheatherActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wheather);
final String url1,url3;
url1="http://api.openweathermap.org/data/2.5/weather?q=";
url3="&mode=xml ";
final EditText etCity=(EditText)findViewById(R.id.etCity);
Button btnView=(Button)findViewById(R.id.btnViewWheather);
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Wheather> wheatherList=new ArrayList<Wheather>();
WheatherHandler wheatherHandler=new WheatherHandler();
String url2=etCity.getText().toString();
try {
URL url = new URL(url1+url2+url3);
//exception can raise Url must be https not http
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.setReadTimeout(15000);
conn.connect();
InputStream is=conn.getInputStream();
//
wheatherList=wheatherHandler.parse(is);
for(Wheather weath:wheatherList)
{
etCity.setText(weath.getTemp()+weath.getCity()+weath.getSunRise());
}
//
}
catch (Exception e)
{
etCity.setText(e.getMessage());
}
}
});
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_wheather, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的课程是这样的:
package com.mubu.wheathertoday.wheathertoday;
import java.util.Date;
/**
* Created by mubashir.gul on 4/6/2015.
*/
public class Wheather {
private String city,country,temp,MinTemp,MaxTemp,tempUnit;
private Date sunRise,sunSet;
public void setCity(String city)
{
this.city=city;
}
public String getCity()
{
return this.city;
}
public void setCountry(String country)
{
this.country=country;
}
public String getCountry()
{
return this.country;
}
public void setTemp(String temp)
{
this.temp=temp;
}
public String getTemp()
{
return this.temp;
}
public void setMaxTemp(String temp)
{
this.MaxTemp=temp;
}
public String getMaxTemp()
{
return this.MaxTemp;
}
public void setMinTemp(String temp)
{
this.MinTemp=temp;
}
public String getMinTemp()
{
return this.MinTemp;
}
public void setTempUnit(String temp)
{
this.tempUnit=temp;
}
public String getTempUnit()
{
return this.tempUnit;
}
public void setSunRise(Date temp)
{
this.sunRise=temp;
}
public Date getSunRise()
{
return this.sunRise;
}
public void setSunSet(Date temp)
{
this.sunSet=temp;
}
public Date getSunSet()
{
return this.sunSet;
}
}
Handler就像这样
package com.mubu.wheathertoday.wheathertoday;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mubashir.gul on 4/6/2015.
*/
public class WheatherHandler {
String text;
List<Wheather> listWheather = new ArrayList<Wheather>();
Wheather wheather;
public List<Wheather> getWheather()
{
return listWheather;
}
public List<Wheather> parse(InputStream is) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser=factory.newPullParser();
parser.setInput(is,null);
int eventType=parser.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT)
{
String tagName=parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagName.equalsIgnoreCase("city")) {
wheather = new Wheather();
wheather.setCity("Golu");
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagName.equalsIgnoreCase("city")) {
wheather.setCity(text);
listWheather.add(wheather);
} else if (tagName.equalsIgnoreCase("city")) {
;
} else if (tagName.equalsIgnoreCase("temperature")) {
wheather.setTemp( text );
}
break;
default:
break;
}
eventType=parser.next();
}
}
catch (Exception e) {
String mubu=e.getMessage();
}
return listWheather;
}
}
我在try语句中放置了try catch,发现我在这个语句中遇到异常。
conn.setDoInput(true); try { conn.connect(); } catch(Exception e) { etCity.setText(e.getMessage()); }
异常是这样的:
尝试调用接口方法&#39; void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhtt p.Request)&#39;在空对象引用上