泽西客户需要帮助 [英] Jersey client help needed
问题描述
大家好,以下代码在Android Studio中完美运行,但在Android平板电脑上使用AIDE引发了NullPointerException - sendRequest函数中出现错误 - 我怀疑它是不兼容的jersey客户端库但我不是Java专家 - 任何想法家伙?如果有一种更简单的方法可以做到这一点,我会全力以赴 - 所有我想做的就是从我本地的本地网络服务器发送和接收json
删除了不相关的代码但会在需要时发布
Hi all, the following code runs perfectly in Android Studio but raises a NullPointerException using AIDE on my android tablet - the error occurs in the sendRequest function - I suspect it's the jersey client library not being compatible but I'm no Java expert - any ideas guys ? If there is an easier way to do this I'm all ears - all im trying to do is send and receive json from a local web server on my lan
irrelevant code removed but will post if needed
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import java.net.*;
class JavaJSONSample
{
private static final String SERVER = "http://10.10.1.11:9000/jsonrpc.js";
static Client client;
static WebResource resource;
public static void main(String[] args)
{
try
{
client = Client.create();
resource = client.resource(SERVER);
resource.accept("application/json");
JSONObject artistrequest = createRequest(null, new String[]{"artists", "0", "-1"});
JSONObject artists = sendRequest(artistrequest);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private static JSONObject sendRequest(JSONObject request)
{
return resource.post(JSONObject.class, request);
}
private static JSONObject createRequest(String playerId, String[] params)
{
JSONObject request = new JSONObject();
try
{
request.put("id", 1L);
request.put("method", "slim.request");
JSONArray requestData = new JSONArray();
requestData.put("-");
JSONArray commandArray = new JSONArray();
for (String param : params)
{
commandArray.put(param);
}
requestData.put(commandArray);
request.put("params", requestData);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return request;
}
}
这是错误
This is the error
java.lang.ExceptionInInitializerError
at com.sun.jersey.core.spi.factory.MessageBodyFactory.getMessageBodyWriterMediaTypes(MessageBodyFactory.java:444)
at com.sun.jersey.api.client.RequestWriter.getMediaType(RequestWriter.java:324)
at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:282)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:217)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:153)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.post(WebResource.java:253)
at JavaJSONSample.sendRequest(Main.java:77)
at JavaJSONSample.main(Main.java:26)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.aide.ui.build.java.RunJavaActivity$1.run(SourceFile:108)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object javax.ws.rs.ext.RuntimeDelegate$HeaderDelegate.fromString(java.lang.String)' on a null object reference
at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
at com.sun.jersey.core.header.MediaTypes.<clinit>(MediaTypes.java:65)
... 14 more
推荐答案
1.run(SourceFile:108)
at java.lang.Thread.run(Thread.java:818)
引起:java.lang.NullPointerException:尝试调用接口方法'java .lang.Object javax.ws.rs.ext.RuntimeDelegate
1.run(SourceFile:108) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object javax.ws.rs.ext.RuntimeDelegate
HeaderDelegate.fromString(java.lang.String)'在空对象引用
at javax.ws.rs .core.MediaType.valueOf(MediaType.java:119)com.s的
un.jersey.core.header.MediaTypes。< clinit>(MediaTypes.java:65)
... 14更多
HeaderDelegate.fromString(java.lang.String)' on a null object reference at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119) at com.sun.jersey.core.header.MediaTypes.<clinit>(MediaTypes.java:65) ... 14 more
这篇关于泽西客户需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!