Java Web服务可在java应用程序中使用,但不在Android应用程序中

时间:2013-05-06 07:27:53

标签: java android web-services noclassdeffounderror

如下面的屏幕截图所示,我已按照this article创建了一个Web服务(在java中)和一个客户端应用程序(android app)。

在文章和客户端应用8文件下的创建Web服务客户端部分下创建 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);

我做错了吗?

任何帮助表示感谢。


screenshot

编辑(1)

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

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

screenshot

编辑(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>

1 个答案:

答案 0 :(得分:0)

最后,在得知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;
    }
  }

感谢您的所有时间和回应!