安卓 RESTful 客户端

标签 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

问题出在哪里?

最佳答案

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

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

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 线程

请注意,您正在为 onPostExecute 定义一个 Long 参数,这是错误的,因为您将其定义为采用 String 类在你的 AsyncTask 定义中

关于安卓 RESTful 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9204134/

相关文章:

android - 带 USB 访问的 ARC?

java - 休息| @生产和@消费: why dont they both get called for same MIME-type

javascript - 是否有 .net Polly 库的任何 JavaScript(Angular)等效项

php - W7 Pro IIS 7.5 覆盖 PHP 位置 : Header

Android Junit 测试失败,出现 "Only the original thread that created a view hierarchy can touch its views."

android - Android Espresso UI 测试中 @Rule 的用途是什么?

无法完成 Java String 到 JSON 的转换

android - Gradle 同步失败 : Cause: com/android/build/gradle/BaseExtension

rest - HATEOAS API 客户端不应使用添加书签的 URL 吗?

php - Yii2 AccessControl 在 Rest API 中使用 HTTPBearerAuth 时给出 Forbidden 403