开发者

Android:About android webservice

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 canno开发者_如何转开发t 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();
}

}


As your code shows you are using KSoap2. In KSoap2 there is the Class ServiceConnectionSE with the following constructor:

public ServiceConnectionSE(String url) throws IOException {
    connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setUseCaches(false);
    connection.setDoOutput(true);
    connection.setDoInput(true);
}

You can change the constructor of the class ServiceConnectionSE as follows. The better way though is to make a copy of the ServiceConnectionSE class (and name it e.g. ServiceProxyConnectionSE) and implement the constructor as follows:

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 );

}

PLEASE NOTE, that you will also have to create a copy of the HTTPTransportSE class (named e.g. HTTPProxyTransportSE) then and change the method getServiceConnection to the new ServiceProxyConnection class!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜