Android的Web服务访问错误 [英] Android webservice access error

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

问题描述

我已经部署Tomcat7服务器上的一个简单的Java Web服务。我已经使用的Axis2作为SOAP引擎(网络服务与; WSDL是OK)
以下是我已经使用Web服务的code

I have a simple java web service deployed on Tomcat7 server. I have used Axis2 as soap engine.(Web service & WSDL is OK) Here is the code I have used for the web service

  public class TestWs {
  public String sayHello(){
         return "Hello Grant";
    }
   }

我已访问Android电子2.2.It这个Web服务是完全OK.But这个节目不是作品为Android 4.0.3
我的Andr​​oid程序的code

I have accessed this web service from Android 2.2.It is perfectly OK.But this program not works for Android 4.0.3 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);
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);          

    SoapSerializationEnvelope envelope = new 
    SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);

    HttpTransportSE ht = new HttpTransportSE(URL);
    try {
        ht.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        String str = response.toString();
        TextView result;
        result = (TextView)findViewById(R.id.textView1);
        result.setText(str);

    } catch (Exception e) {
        e.printStackTrace();
        TextView result;
        result = (TextView)findViewById(R.id.textView1);
        result.setText("Error!");

    }
  }
 }

程序正确安装模拟器。但仿真器显示从catch块设置消息。什么是错误?我该如何纠正?
谢谢!

Program correctly installed on Emulator. But emulator shows the message set from catch block. What is the error ??? How can I correct it ?? Thanks!

推荐答案

在Honeycomb和Ice Cream Sandwich的(即Android 3.0以上版本),您无法连接到互联网在主线程(的onCreate()的onPause( ),onResume()等),你必须,而不是开始一个新的线程。为什么这种情况已经改变的原因是因为网络运营可以使应用程序等待很长一段时间,和如果你再在主线程中运行它们,整个应用程序没有响应。如果您尝试从主线程连接, Android将抛出一个NetworkOnMainThreadException。

In Honeycomb and Ice Cream Sandwich (i.e. Android 3.0+) , you cannot connect to the internet in the main thread (onCreate(), onPause(), onResume() etc.), and you have to instead start a new thread. The reason why this has changed is because network operations can make the app wait for a long time, and if you're running them in the main thread, the whole application becomes unresponsive. If you try to connect from the main thread, Android will throw a NetworkOnMainThreadException.

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

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