Android的RESTful客户端 [英] Android RESTful client

查看:229
本文介绍了Android的RESTful客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RESTful Web服务,我想从Android的访问。

I have a RESTful web service and I want to access it from Android.

public class AndroidClientActivity extends Activity {

private EditText text;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (EditText) findViewById(R.id.editText1);
}

public void myClickHandler(View view){
    switch (view.getId()) {
    case R.id.button1:
        MyClient c = new MyClient();
        c.execute(text);
        break;
    }

}
}


public class MyClient extends AsyncTask<EditText, Integer, String> {

protected String doInBackground(EditText... strings) {

        WebResource wbr;
        Client client = Client.create();
        wbr = client.resource("http://my.ip.address:8080/MazeService/rest/service/hello");  
        String result = wbr.queryParam("number", "10").accept(MediaType.APPLICATION_JSON).get(String.class);
        strings[0].setText(result);
    return result;
}

protected void onProgressUpdate(Integer... progress) {
}

protected void onPostExecute(Long result) {
}
}

我得到这个堆栈跟踪在LogCat中:

I get this stacktrace in LogCat:

02-09 00:06:38.593: E/AndroidRuntime(795): Caused by: java.lang.NullPointerException
02-09 00:06:38.593: E/AndroidRuntime(795):  at javax.ws.rs.core.MediaType.valueOf(MediaType.java:119)
02-09 00:06:38.593: E/AndroidRuntime(795):  at com.sun.jersey.api.client.ClientResponse.getType(ClientResponse.java:615)
02-09 00:06:38.593: E/AndroidRuntime(795):  at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:532)
02-09 00:06:38.593: E/AndroidRuntime(795):  at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
02-09 00:06:38.593: E/AndroidRuntime(795):  at com.sun.jersey.api.client.WebResource.handle(WebResource.java:674)
02-09 00:06:38.593: E/AndroidRuntime(795):  at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
02-09 00:06:38.593: E/AndroidRuntime(795):  at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:503)
02-09 00:06:38.593: E/AndroidRuntime(795):  at com.maze.client.MyClient.doInBackground(MyClient.java:19)
02-09 00:06:38.593: E/AndroidRuntime(795):  at com.maze.client.MyClient.doInBackground(MyClient.java:1)
02-09 00:06:38.593: E/AndroidRuntime(795):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
02-09 00:06:38.593: E/AndroidRuntime(795):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-09 00:06:38.593: E/AndroidRuntime(795):  ... 5 more

问题出在哪里?

Where is the problem?

推荐答案

我觉得你试图访问从doInBackground方法UI线程,这不是一个好地方,要做到这一点,而不是改变文本 onPostExecute 这样的方法,你可以安全地访问UI线程像这样

I think you are trying to access the UI thread from the doInBackground method and it's not a good place to do that instead change the text in onPostExecute method like this you can access the ui thread safely like this

先定义你的AsyncTask类的私有类在你的Activity类

first define you AsyncTask class as a private class in your Activity class

private class MyClient extends AsyncTask<EditText, Void, String> { 

protected String doInBackground(EditText... strings) {

        WebResource wbr;
        Client client = Client.create();
        wbr = client.resource("http://my.ip.address:8080/MazeService/rest/service/hello");  
        String result = wbr.queryParam("number", "10").accept(MediaType.APPLICATION_JSON).get(String.class);

    return result;
}
    protected void onPostExecute(String result) {
    if(result != null)
    text.setText(result);

    }
}

然后访问UI线程的 onPostExecute

注意,你要定义一个参数为 onPostExecute 和多数民众错误的,因为你把它定义采取字符串类在你的AsyncTask定义

note that you are defining a Long parameter for the onPostExecute and thats wrong since you define it to take a String class in your AsyncTask definition

这篇关于Android的RESTful客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆