首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android REST REST服务

Android REST REST服务
EN

Stack Overflow用户
提问于 2015-04-18 19:54:21
回答 2查看 343关注 0票数 1

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

代码语言:javascript
复制
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上运行它时得到的错误

代码语言:javascript
复制
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个小时了。我也尝试了另一种选择,但没有奏效!如果你对此有其他选择,请让我知道。

EN

回答 2

Stack Overflow用户

发布于 2015-04-18 19:57:37

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

代码语言:javascript
复制
 <uses-permission android:name="android.permission.INTERNET" /> 
票数 0
EN

Stack Overflow用户

发布于 2015-04-19 02:18:49

实际上,当您只显示用于存储结果和发出post请求的对象时,要理解问题出在哪里太复杂了。这已经被弃用了,但在Android中仍然可以很好地工作,并且可能会对你发现错误很有用。

代码语言:javascript
复制
    try {
        //Create an HTTP client
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("https://www...."); //your url as String
        //use HttpPost instead HttpGet if you need the post method
        //Perform the request and check the status code
        HttpResponse response = client.execute(get);
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();

            try {
                //Read the server response 
                Reader reader = new InputStreamReader(content);

                //get your data from the reader

                content.close();
            } catch (Exception ex) {
                Log.e(TAG, "Failed with: " + ex);
            }
        } else {
            Log.e(TAG, "Server responded with status code: " + statusLine.getStatusCode());
        }
    } catch (Exception ex) {
        Log.e(TAG, "Failed to send HTTP POST request due to: " + ex);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29716970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档