Android的Web服务访问 [英] Android web service access

查看:142
本文介绍了Android的Web服务访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已访问Android电子2.2.It Web服务是完全OK.I改变了我的程序为Android 4.0.3访问同一网页service.But这不起作用。
我的Andr​​oid程序的code

I have accessed a web service from Android 2.2.It is perfectly OK.I changed my program for Android 4.0.3 to access same web service.But this doesn't work. Code of my Android program

 import org.ksoap2.SoapEnvelope;
 import org.ksoap2.serialization.SoapObject;
 import org.ksoap2.serialization.SoapPrimitive;
 import org.ksoap2.serialization.SoapSerializationEnvelope;
 import org.ksoap2.transport.HttpTransportSE;

 import android.widget.TextView;
 import android.app.Activity;
 import android.os.Bundle;

 public class AndroidWSClientActivity extends Activity {

private static final String SOAP_ACTION = "http://ws.android4.com/";
private static final String METHOD_NAME = "sayHello";
private static final String NAMESPACE = "http://ws.android4.com/sayHello/";
private static final String URL = "http://175.157.45.91:8085/ForAndroid4/services/TestWs?wsdl";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Thread networkThread = new Thread() {
    @Override
    public void run() {
       try {
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);          
         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.setOutputSoapObject(request);

         HttpTransportSE ht = new HttpTransportSE(URL);
         ht.call(SOAP_ACTION, envelope);
         final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
         final String str = response.toString();

         runOnUiThread (new Runnable(){

          public void run() {
              TextView result;
               result = (TextView)findViewById(R.id.textView1);
               result.setText(str);
              }
           }

          } 

   catch (Exception e) {
            e.printStackTrace();
        }
    }
  };
}
}

我已经加入网络的权限也。
该程序安装在仿真器正常。但不显示结果。没有任何错误showing.Eclipse说networkThread不被使用。我怎样才能解决这个?

I have added internet permission also. This program installing on emulator correctly. But doesn't show the result. No any errors showing.Eclipse says that "networkThread" is not used. How can I correct this ??

谢谢!

推荐答案

它看起来像你从来没有开始networkThread。您需要这个在你的onCreate的最后():

It looks like you never started the networkThread. You would need this at the very end of your onCreate():

networkThread.start();

这就是说,你会很好地使用AsyncTask的做这种类型的事情。我想你会发现它更容易,一旦你得到了它的窍门。

That being said, you would do well to use AsyncTask to do this type of thing. I think you will find it much easier once you get the hang of it.

http://developer.android.com/reference/android/os/ AsyncTask.html

这篇关于Android的Web服务访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆