Java应用程序中的Java web服务可消费,但android应用程序中不可消费
如下面的屏幕截图所示,我通过以下方式创建了一个web服务(java)和一个客户端应用程序(android应用程序) 在文章的“创建web服务客户机”部分和“客户机应用程序”部分中创建Ant构建文件后,我还收到了构建成功消息,生成的文件如下图(2)所示 现在当我编写Java应用程序中的Java web服务可消费,但android应用程序中不可消费,java,android,web-services,noclassdeffounderror,Java,Android,Web Services,Noclassdeffounderror,如下面的屏幕截图所示,我通过以下方式创建了一个web服务(java)和一个客户端应用程序(android应用程序) 在文章的“创建web服务客户机”部分和“客户机应用程序”部分中创建Ant构建文件后,我还收到了构建成功消息,生成的文件如下图(2)所示 现在当我编写HelloWebService=newhellowebservice()时在客户端应用程序中,应用程序崩溃,我得到以下异常: java.lang.NoClassDefFoundError:com.mycompany.service.cl
HelloWebService=newhellowebservice()时代码>在客户端应用程序中,应用程序崩溃,我得到以下异常:
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-sc:\Android\workspace\WebServiceDemo\src-pcom.mycompany.service.clienthttp://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>
在得知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;
}
}
class RequestTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…uri){
HttpClient HttpClient=getNewHttpClient();
HttpResponse响应;
字符串add=“{\”MemberNo\:\”“+uri[0]+“\”}”;
HttpPost postMethod=新的HttpPost(wsdl);//+add
字符串responseString=null;
试一试{
addHeader(“内容类型”、“应用程序/json”);
HttpEntity实体=新的StringEntity(添加);
方法后。集合实体(实体);
response=httpclient.execute(postMethod);
StatusLine StatusLine=response.getStatusLine();
if(statusLine.getStatusCode()==HttpStatus.SC\u OK){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString=out.toString();
}否则{
response.getEntity().getContent().close();
抛出新IOException(statusLine.getReasonPhrase());
}
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
回报率;
}
}
谢谢大家的时间和回复 您是否已将该服务移植到公共IP上?或者当你运行你的应用程序时,这项服务应该在你的本地浏览器上运行,我的意思是它应该启动。是的,它正在运行,这就是为什么我可以在Java应用程序中使用它,但不能在android应用程序中使用它的原因:(你应该发布调用此web服务的android代码。你还必须在你的android-manifest中设置INTERNET
权限访问。是的,我提到了INTERNET权限。android应用程序中使用web服务的代码与我在java应用程序中使用的代码完全相似(有疑问)。请检查编辑的问题。是否有任何库您忘记添加到生成路径中,接下来是什么hellowebservice.java
是扩展的?简单类还是扩展某个父类?