当我的电脑尝试连接互联网时,我必须设置代理,用户名和密码。我将它们设置在我的Android虚拟机中并且它可以工作。(虚拟机可以访问互联网)。但是当我运行我的应用程序,它无法访问Internet。 有人帮帮我吗?先谢谢!!
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();
}
}
答案 0 :(得分:3)
正如您的代码所示,您正在使用KSoap2。在KSoap2中,类ServiceConnectionSE
具有以下构造函数:
public ServiceConnectionSE(String url) throws IOException {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
}
您可以按如下方式更改类ServiceConnectionSE
的构造函数。更好的方法是复制ServiceConnectionSE
类(并将其命名为ServiceProxyConnectionSE
)并按如下方式实现构造函数:
public ServiceProxyConnectionSE(
String url, String user, String passwd ) throws IOException {
String s = user + ":" + passwd;
new Base64();
String strBase64 = "Basic " + Base64.encode( s.getBytes() );
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput( true );
connection.setRequestProperty( "Authorization", strBase64 );
}
请注意,您还必须创建HTTPTransportSE
类的副本(名为HTTPProxyTransportSE
),然后将方法getServiceConnection
更改为新的ServiceProxyConnection
类!