android怎么访问天气webservice,用网上找的链接都不行啊
====================问题描述====================
做个天气预报的小测试用网上找的链接怎么不行,有做过的指导下。感激不尽!!
求用法或可用链接!!!!!!!!!!
http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx
和
http://www.webxml.com.cn/webservices/weatherwebservice.asmx
都是不行的!跟着网上例子做的。
错误:
11-13 19:27:10.545: W/System.err(29917): java.net.UnknownHostException: Unable to resolve host "webservice.webxml.com.cn": No address associated with hostname
11-13 19:27:10.545: W/System.err(29917): at java.net.InetAddress.lookupHostByName(InetAddress.java:426)
11-13 19:27:10.545: W/System.err(29917): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-13 19:27:10.545: W/System.err(29917): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
11-13 19:27:10.545: W/System.err(29917): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
11-13 19:27:10.545: W/System.err(29917): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
11-13 19:27:10.545: W/System.err(29917): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
11-13 19:27:10.550: W/System.err(29917): at com.testtv.core.WeatherActivity.getWeather(WeatherActivity.java:102)
11-13 19:27:10.550: W/System.err(29917): at com.testtv.core.WeatherActivity$2$1.run(WeatherActivity.java:76)
11-13 19:27:10.550: W/System.err(29917): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-13 19:27:10.550: W/System.err(29917): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-13 19:27:10.550: W/System.err(29917): at java.lang.Thread.run(Thread.java:856)
11-13 19:27:10.550: W/System.err(29917): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
11-13 19:27:10.550: W/System.err(29917): at libcore.io.Posix.getaddrinfo(Native Method)
11-13 19:27:10.550: W/System.err(29917): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
11-13 19:27:10.550: W/System.err(29917): at java.net.InetAddress.lookupHostByName(InetAddress.java:411)
代码:
package com.testtv.core;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class WeatherActivity extends Activity {
/** Called when the activity is first created. */
private Button okButton;
/** Called when the activity is first created. */
private static final String NAMESPACE = "http://WebXml.com.cn/";
// WebService地址
private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
private static final String METHOD_NAME = "getWeatherbyCityName";
private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
private String weatherToday;
private SoapObject detail;
private Handler webHandler;
private final static String TAG = "WeatherActivity";
private final static int SHOW_WEATHER=10;
private Executor webExcutor;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
this.webHandler=new Handler()
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what==SHOW_WEATHER)
{
try {
parseWeather(detail);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
okButton = (Button) this.findViewById(R.id.btn_Search);
okButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
final String city = "北京";
if(webExcutor==null)
{
webExcutor=Executors.newFixedThreadPool(1);
}
webExcutor.execute(new Runnable() {
public void run() {
// TODO Auto-generated method stub
getWeather(city);
}
});
}
});
}
public void getWeather(String cityName) {
try {
System.out.println("rpc------");
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
System.out.println("rpc" + rpc);
System.out.println("cityName is " + cityName);
rpc.addProperty("theCityName", cityName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
HttpTransportSE ht = new HttpTransportSE(URL);
// AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;
ht.call(SOAP_ACTION, envelope);
// ht.call(null, envelope);
// SoapObject result = (SoapObject)envelope.bodyIn;
// detail = (SoapObject)
// result.getProperty("getWeatherbyCityNameResult");
detail = (SoapObject) envelope.getResponse();
Log.d(TAG, detail.toString());
Message msg = new Message();
msg.what=SHOW_WEATHER;
webHandler.sendMessage(msg);
// Toast.makeText(this, detail.toString(), Toast.LENGTH_LONG).show();
return;
} catch (Exception e) {
e.printStackTrace();
}
}
private void parseWeather(SoapObject detail) throws UnsupportedEncodingException {
String date = detail.getProperty(6).toString();
weatherToday = "今天:" + date.split("")[0];
weatherToday = weatherToday + "\n天气:" + date.split("")[1];
weatherToday = weatherToday + "\n气温:" + detail.getProperty(5).toString();
weatherToday = weatherToday + "\n风力:" + detail.getProperty(7).toString() + "\n";
System.out.println("weatherToday is " + weatherToday);
Toast.makeText(this, weatherToday, Toast.LENGTH_LONG).show();
}
}
都是网上找的,大拿们速来救火!!
====================解决方案1====================
这两个接口都已经失效了,没法访问,你找个能用的,用浏览器就可以测试
====================解决方案2====================
楼主现在解决了吗?求解
====================解决方案3====================
网络权限开了吗
====================解决方案4====================
看看有没有返回数据就清楚了
====================解决方案5====================
Unable to resolve host "webservice.webxml.com.cn": No address associated with hostname
这个意思是域名解析不正常,确信网络OK?
====================解决方案6====================
这个网站现在应用广泛吗? 看了一下他们的接口文档,真心觉得不怎么样啊,没有一点做开放平台的水准。。。。,
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。