Android RESTful客户端

时间:2012-02-09 01:12:14

标签: android rest

我有一个RESTful网络服务,我想从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中得到了这个堆栈跟踪:

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

问题出在哪里?

2 个答案:

答案 0 :(得分:4)

我认为您正在尝试从doInBackground方法访问UI线程,并且它不是一个好地方,而是更改onPostExecute方法中的文本,这样您可以像这样安全地访问ui线程

首先将AsyncTask类定义为Activity类中的私有类

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);

    }
}

然后从onPostExecute

访问UI线程

请注意,您正在为Long定义onPostExecute参数,这是错误的,因为您将其定义为在AsyncTask定义中使用String < / p>

答案 1 :(得分:0)

永远不要使用AsynTask来执行网络请求或任何需要持久化的请求。 Async Task与您的活动密切相关,如果用户在重新创建应用程序后更改了屏幕方向,则AsyncTask将停止。

我建议您使用服务模式与Intent Service和ResultReceiver。看看RESTDroid。它是一个库,允许您异步执行任何类型的REST请求,并通过请求监听器实现Virgil Dobjanschi的服务模式来通知您的UI。