gpt4 book ai didi

安卓 RESTful 客户端

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:07 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com