我正在为我构建的Web服务创建一个简单的Android客户端,以便从XML获取List。
但是我得到了这个堆栈跟踪:
8 16:07:39.272: E/ClientResponse(8460): A message body reader for Java class java.util.List, and Java type java.util.List<xx.xxxx.xxx.xxx.xxxx.xxxx.MessdatenPv>, and MIME media type application/xml was not found
02-18 16:07:39.272: E/ClientResponse(8460): The registered message body readers compatible with the MIME media type are:
02-18 16:07:39.272: E/ClientResponse(8460): */* ->
02-18 16:07:39.272: E/ClientResponse(8460): com.sun.jersey.core.impl.provider.entity.StringProvider
02-18 16:07:39.272: E/ClientResponse(8460): com.sun.jersey.core.impl.provider.entity.ReaderProvider
02-18 16:07:39.272: W/dalvikvm(8460): threadid=16: thread exiting with uncaught exception (group=0x41ed2930)
02-18 16:07:39.272: E/AndroidRuntime(8460): FATAL EXCEPTION: Thread-764
02-18 16:07:39.272: E/AndroidRuntime(8460): com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class java.util.List, and Java type java.util.List<xx.xxxx.xx.xx.xxxx.xxx.MessdatenPv>, and MIME media type application/xml was not found
02-18 16:07:39.272: E/AndroidRuntime(8460): at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:549)
02-18 16:07:39.272: E/AndroidRuntime(8460): at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:523)
02-18 16:07:39.272: E/AndroidRuntime(8460): at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
02-18 16:07:39.272: E/AndroidRuntime(8460): at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:74)
02-18 16:07:39.272: E/AndroidRuntime(8460): at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:508)
02-18 16:07:39.272: E/AndroidRuntime(8460): at com.example.monitor.thread.Login.verifyLogin(Login.java:74)
02-18 16:07:39.272: E/AndroidRuntime(8460): at com.example.monitor.activity.LoginActivity$3.run(LoginActivity.java:152)
02-18 16:07:39.272: E/AndroidRuntime(8460): at java.lang.Thread.run(Thread.java:856)
以下是客户:
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource webResource = client.resource(getBaseURI());
List<MessdatenPv> listMessdatenPv = webResource.path("test").path("helloworld/getMessdatenPv").accept(MediaType.APPLICATION_XML).get(new GenericType<List<MessdatenPv>>() {});
Webservice:
@GET
@Path("/getMessdatenPv")
@Produces(MediaType.APPLICATION_XML)
public List<MessdatenPvCommon> getMessdatenPv() throws Exception {
List<MessdatenPvCommon> messdatenPvList = getMessdaten();
return messdatenPvList;
}
ServiceFinder $ ServiceIteratorProvider
package com.example.monitor.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import android.util.Log;
import com.sun.jersey.spi.service.ServiceFinder.ServiceIteratorProvider;
public class AndroidServiceIteratorProvider<T> extends ServiceIteratorProvider<T>
{
private static final String TAG = AndroidServiceIteratorProvider.class.getSimpleName();
private static final String MESSAGE = "Unable to load provider";
private static final HashMap<String, String[]> SERVICES = new HashMap<String, String[]>();
private static final String[] com_sun_jersey_spi_HeaderDelegateProvider = {"com.sun.jersey.core.impl.provider.header.MediaTypeProvider",
"com.sun.jersey.core.impl.provider.header.StringProvider"};
private static final String[] com_sun_jersey_spi_inject_InjectableProvider = {};
private static final String[] javax_ws_rs_ext_MessageBodyReader = {"com.sun.jersey.core.impl.provider.entity.StringProvider",
"com.sun.jersey.core.impl.provider.entity.ReaderProvider"};
private static final String[] javax_ws_rs_ext_MessageBodyWriter = {"com.sun.jersey.core.impl.provider.entity.StringProvider",
"com.sun.jersey.core.impl.provider.entity.ReaderProvider"};
static
{
SERVICES.put("com.sun.jersey.spi.HeaderDelegateProvider", com_sun_jersey_spi_HeaderDelegateProvider);
SERVICES.put("com.sun.jersey.spi.inject.InjectableProvider", com_sun_jersey_spi_inject_InjectableProvider);
SERVICES.put("javax.ws.rs.ext.MessageBodyReader", javax_ws_rs_ext_MessageBodyReader);
SERVICES.put("javax.ws.rs.ext.MessageBodyWriter", javax_ws_rs_ext_MessageBodyWriter);
SERVICES.put("jersey-client-components", new String[] {});
SERVICES.put("com.sun.jersey.client.proxy.ViewProxyProvider", new String[] {});
}
@SuppressWarnings("unchecked")
@Override
public Iterator<Class<T>> createClassIterator(Class<T> service, String serviceName, ClassLoader loader, boolean ignoreOnClassNotFound)
{
String[] classesNames = SERVICES.get(serviceName);
int length = classesNames.length;
ArrayList<Class<T>> classes = new ArrayList<Class<T>>(length);
for (int i = 0; i < length; i++)
{
try
{
classes.add((Class<T>) Class.forName(classesNames[i]));
}
catch (ClassNotFoundException e)
{
Log.v(TAG, MESSAGE, e);
}
}
return classes.iterator();
}
@Override
public Iterator<T> createIterator(Class<T> service, String serviceName, ClassLoader loader, boolean ignoreOnClassNotFound)
{
String[] classesNames = SERVICES.get(serviceName);
int length = classesNames.length;
ArrayList<T> classes = new ArrayList<T>(length);
for (int i = 0; i < length; i++)
{
try
{
classes.add(service.cast(Class.forName(classesNames[i]).newInstance()));
}
catch (IllegalAccessException e)
{
Log.v(TAG, MESSAGE, e);
}
catch (InstantiationException e)
{
Log.v(TAG, MESSAGE, e);
}
catch (ClassNotFoundException e)
{
Log.v(TAG, MESSAGE, e);
}
}
return classes.iterator();
}
}
这种麻烦只发生在eclipse adt中,在eclipse kepler中它起作用..
任何人都可以帮我这个。
答案 0 :(得分:1)
默认情况下,JAXB不包含在Android中。看看this question的答案。我建议你和这些答案一样:使用JSON(Jackson)而不是XML,或者在客户端使用不同的XML解析(Java DOM,XPath,SimpleXML)/绑定(XStream)库。