1

如下图所示,我按照这篇文章创建了一个 Web 服务(在 java 中)和一个客户端应用程序(android 应用程序) 。

在文章中创建 Web 服务客户端部分下创建Ant 构建文件后,我还收到BUILD SUCCESSFUL消息,在客户端应用程序下生成 8 个文件,如下图 (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>
4

1 回答 1

0

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

我使用了类似的东西:

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-06-11T09:35:22.257 回答