java - Android REST Web 服务

标签 java android web-services rest

我有一个在java项目上完美运行的代码,但是当我在android项目上使用相同的代码时,它会抛出错误,这里是代码。(注意我无法共享链接和参数,问题不在那里。链接是正确的。代码正在工作,我在eclipse中的java项目中得到结果,但在android项目中同样失败)

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
try{
        Client client=Client.create();  
        WebResource webResource2 = client.resource("some...link");

        WSDetPojo wsdetpojo = new WSDetPojo(); // some class
        wsdetpojo.parameter1("parameter1");
        wsdetpojo.parameter2("parameter2");
        wsdetpojo.parameter3("parameter3");
        wsdetpojo.parameter4("parameter4");


        ObjectMapper om=new ObjectMapper();

        ClientResponse response2 = webResource2.type("application/json")
        .post(ClientResponse.class,om.writeValueAsString(wsdetpojo)); // here is the problem <==

        System.out.println("response of sms ==> "+response2);
        if (response2.getStatus() != 201) 
        {
            throw new RuntimeException("Failed : HTTP error code : "+ response2.getStatus());

        }

        String output2 = response2.getEntity(String.class);
        System.out.println("output :\n" +output2);

    }catch(Exception ex){
        ex.printStackTrace();
    }

现在这是我在 android 上运行它时遇到的错误

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.api.client.PartialRequestBuilder.type(PartialRequestBuilder.java:92)
    at com.sun.jersey.api.client.WebResource.type(WebResource.java:347)
    at co.example.punerto.classes.NicClient.sendData(NicClient.java:43)
    at com.example.punerto.Activity.ActivityLearnLicAppointment_Tab6$uploadFrom.doInBackground(ActivityLearnLicAppointment_Tab6.java:303)
    at com.example.punerto.Activity.ActivityLearnLicAppointment_Tab6$uploadFrom.doInBackground(ActivityLearnLicAppointment_Tab6.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)

请帮忙!!卡在这上面已经花了8个小时了。我也尝试过替代方法没有用!如果您有其他选择,请告诉我。

最佳答案

您是否设置了所需的 Android 权限?

 <uses-permission android:name="android.permission.INTERNET" /> 

关于java - Android REST Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29716970/

相关文章:

java - Maven 3.2.5 中的 Flexmojo 支持

java - Tween 引擎 - Tween.to 忽略目标值

android - onPostExecute() 不能使用 MainActivity 元素

android - 哪里可以找到Android的原始资源图片

Phpunit,模拟 SoapClient 是有问题的(模拟魔术方法)

c# - 如何更新代码中的客户端端点绑定(bind)

java - 我的代码给出了 java.lang.StringIndexOutOfBoundsException 并且命令以非零状态退出

java - Rails 应用程序中的 unicode 字符显示为 ???在mysql数据库中,

web-services - i18n/多语言应该在哪一层处理?

android - Android Studio 中的默认 AVD