Android:关于android webservice

发布于 2024-12-04 11:22:28 字数 2476 浏览 0 评论 0原文

当我的计算机尝试连接互联网时,我必须设置代理、用户名和密码。我在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2024-12-11 11:22:28

正如您的代码显示您正在使用 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 类!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文