【问题标题】:Java web service consumable in java app but not in android appJava Web 服务可在 Java 应用程序中使用,但不能在 android 应用程序中使用
【发布时间】:2013-04-29 22:21:13
【问题描述】:

如下图所示,我通过this article创建了一个web服务(java)和一个客户端应用程序(android app)。

在文章中的 创建 Web 服务客户端 部分和客户端应用程序 8 文件下创建 Ant 构建文件 后,我还收到了 BUILD SUCCESSFUL 消息生成如下图(2)所示。

现在,当我在客户端应用程序中编写 HelloWebService service = new HelloWebService(); 时,应用程序崩溃并出现以下异常:
java.lang.NoClassDefFoundError: com.mycompany.service.client.HelloWebService

但是如果我尝试在 Java 应用程序中使用相同的 Web 服务,如下所示,它可以工作:

HelloWebService service = new HelloWebService();
com.myservice.service.client.HelloWeb helloWeb = service.getHelloWebPort();
String response = helloWeb.sayGreeting(input);

我做错了什么吗?

任何帮助表示赞赏。


编辑 (1)

虽然我对这两个应用程序使用类似的命令来生成所需的 Web 服务 java 文件,但两个应用程序的结构不同。在 android 应用程序中,文件在 xml 目录下生成。

命令是:wsimport -keep -s C:\Android\workspace\WebServiceDemo\src -p com.mycompany.service.client http://localhost:9898/HelloWeb?wsdl

编辑 (2)

HelloWebService.java

package com.mycompany.service.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.6 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "HelloWebService", targetNamespace = "http://service.mycompany.com/", wsdlLocation = "http://localhost:9898/HelloWeb?wsdl")
public class HelloWebService
    extends Service
{

    private final static URL HELLOWEBSERVICE_WSDL_LOCATION;
    private final static Logger logger = Logger.getLogger(com.mycompany.service.client.HelloWebService.class.getName());

    static {
        URL url = null;
        try {
            URL baseUrl;
            baseUrl = com.mycompany.service.client.HelloWebService.class.getResource(".");
            url = new URL(baseUrl, "http://localhost:9898/HelloWeb?wsdl");
        } catch (MalformedURLException e) {
            logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:9898/HelloWeb?wsdl', retrying as a local file");
            logger.warning(e.getMessage());
        }
        HELLOWEBSERVICE_WSDL_LOCATION = url;
    }

    public HelloWebService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public HelloWebService() {
        super(HELLOWEBSERVICE_WSDL_LOCATION, new QName("http://service.mycompany.com/", "HelloWebService"));
    }

    /**
     * 
     * @return
     *     returns HelloWeb
     */
    @WebEndpoint(name = "HelloWebPort")
    public HelloWeb getHelloWebPort() {
        return super.getPort(new QName("http://service.mycompany.com/", "HelloWebPort"), HelloWeb.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns HelloWeb
     */
    @WebEndpoint(name = "HelloWebPort")
    public HelloWeb getHelloWebPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://service.mycompany.com/", "HelloWebPort"), HelloWeb.class, features);
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webservicedemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.webservicedemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

  • 您是否将服务移植到公共 IP 上?或者当您运行您的应用程序时,该服务应该在您的本地浏览器上运行,我的意思是它应该启动。
  • 是的,它正在运行,这就是为什么我可以在 Java 应用程序中使用它,但不能在 android 应用程序中使用它:(
  • 您应该发布调用此 Web 服务的 android 代码。您还必须在您的 android-manifest 中设置 INTERNETpermission 访问权限。
  • 是的,我已经提到了 Internet 权限。 android 应用程序中用于使用 Web 服务的代码与我用于 java 应用程序的代码完全相似(写有问题)。请检查已编辑的问题。
  • 是否有任何你忘记在构建路径中添加的库,接下来hellowebservice.java 是什么扩展?简单的类还是扩展一些父类?

标签: java android web-services noclassdeffounderror


【解决方案1】:

在得知 Android 不支持 javax.* 包后,我最终放弃了这种方法。

我使用了类似的东西:

class RequestTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = getNewHttpClient();
        HttpResponse response;
        String add = "{\"MemberNo\":\"" + uri[0] + "\"}";
        HttpPost postMethod = new HttpPost(wsdl);// + add
        String responseString = null;
        try {
            postMethod.addHeader("Content-Type", "application/json");
            HttpEntity entity = new StringEntity(add);
            postMethod.setEntity(entity);
            response = httpclient.execute(postMethod);

            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else {
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseString;
    }
  }

感谢大家的时间和回复!

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多