Android:关于android webservice
当我的计算机尝试连接互联网时,我必须设置代理、用户名和密码。我在我的 Android 虚拟机中设置它们并且它可以工作。(虚拟机可以访问互联网)。但是当我运行我的应用程序时,它无法访问互联网。 有人帮助我吗?提前谢谢!
public class wsActivity extends Activity {
private static final String mNameSpace = "http://WebXml.com.cn/";
private static final String mMethodName = "getWeatherbyCityName";
private static final String mUrl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdl";
private static final String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
private String weatherToday = null;
private SoapObject details;
private Button mBtnSearch;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtnSearch = (Button)findViewById(R.id.btn_search);
mBtnSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getWeather("北京");
}
});
}
/**
* getWeather(String cityName)
*
*/
public void getWeather(String cityName){
SoapObject rpc = new SoapObject(mNameSpace, mMethodName);
rpc.addProperty("theCityName", cityName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
HttpTransportSE ht = new HttpTransportSE(mUrl);
//AndroidHttpTransport ht = new AndroidHttpTransport(mUrl);
ht.debug = true;
Log.d("getWeather","path:"+ht.getPath());
try {
ht.call(SOAP_ACTION, envelope);
details = (SoapObject) envelope.getResponse();
Log.d("getWeather",details.toString());
parseWeather(details);
return;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
/**
* parseWeather(SoapObject details)
*
*/
public 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";
Toast.makeText(this, weatherToday, Toast.LENGTH_LONG).show();
}
}
I have to set proxy,username and password when my computer try to connect with the Internet.And I set them in my Android Virtual machine and it works.(The virtual machine can access the Internet).But when i run my app,it cannot access the Internet.
Anyone help me?Thanks in advance!!
public class wsActivity extends Activity {
private static final String mNameSpace = "http://WebXml.com.cn/";
private static final String mMethodName = "getWeatherbyCityName";
private static final String mUrl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdl";
private static final String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";
private String weatherToday = null;
private SoapObject details;
private Button mBtnSearch;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtnSearch = (Button)findViewById(R.id.btn_search);
mBtnSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getWeather("北京");
}
});
}
/**
* getWeather(String cityName)
*
*/
public void getWeather(String cityName){
SoapObject rpc = new SoapObject(mNameSpace, mMethodName);
rpc.addProperty("theCityName", cityName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
HttpTransportSE ht = new HttpTransportSE(mUrl);
//AndroidHttpTransport ht = new AndroidHttpTransport(mUrl);
ht.debug = true;
Log.d("getWeather","path:"+ht.getPath());
try {
ht.call(SOAP_ACTION, envelope);
details = (SoapObject) envelope.getResponse();
Log.d("getWeather",details.toString());
parseWeather(details);
return;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
/**
* parseWeather(SoapObject details)
*
*/
public 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";
Toast.makeText(this, weatherToday, Toast.LENGTH_LONG).show();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您的代码显示您正在使用 KSoap2。在 KSoap2 中,类
ServiceConnectionSE
具有以下构造函数:您可以按如下方式更改类
ServiceConnectionSE
的构造函数。更好的方法是复制ServiceConnectionSE
类(并将其命名为ServiceProxyConnectionSE
)并按如下方式实现构造函数:请注意 ,您还必须创建
HTTPTransportSE
类的副本(例如命名为HTTPProxyTransportSE
),然后将方法getServiceConnection
更改为新的ServiceProxyConnection
类!As your code shows you are using KSoap2. In KSoap2 there is the Class
ServiceConnectionSE
with the following constructor:You can change the constructor of the class
ServiceConnectionSE
as follows. The better way though is to make a copy of theServiceConnectionSE
class (and name it e.g.ServiceProxyConnectionSE
) and implement the constructor as follows:PLEASE NOTE, that you will also have to create a copy of the
HTTPTransportSE
class (named e.g.HTTPProxyTransportSE
) then and change the methodgetServiceConnection
to the newServiceProxyConnection
class!