我是 Android 开发新手,在发出 asnyc rest web 请求时遇到以下设计问题。在我的主要 Activity 中,我有一个使用异步任务执行请求的按钮:
public class MainActivity extends ActionBarActivity {
final Context context=this;
.......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Button btn = (Button)findViewById(R.id.myshowbutton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WebRequest wr=new WebRequest(view.getRootView());
wr.execute("http://www.cheesejedi.com/rest_services/get_big_cheese?level=1");
}
});
}
在单独的 java 文件中的异步任务类:
public class WebRequest extends AsyncTask<String, String, String> {
private View view;
public WebRequest(View v) {
this.view = v;
}
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
EditText et=(EditText)view.findViewById(R.id.editText);
et.setText(result);
}
}
如您所见,我将 EditText 框设置为返回的 JSON。我将 Root View 的引用传递给我的 WebRequest 类,以便我可以获取 EditText 框。
有更好的方法吗?看起来不是很优雅。也许是一个不错的网络请求库?
最佳答案
这种方式很好用,知道怎么写就很好了。您可以做几件事来改进这一点,还有一个库可以用来大大简化您的代码。
选项 1:改进您的代码
首先,您可以使用 Callback
在 Activity
的 EditText
中设置文本,而不是:
在 WebRequest
中更改这些行:
private View view;
public WebRequest(View v) {
this.view = v;
}
到
public interface Callback {
public void call(String result);
}
private Callback callback;
public WebRequest(Callback c) {
this.callback = callback;
}
然后在onPostExecute
中调用:
callback.call(result);
现在,当您启动网络服务时,您的调用将如下所示:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WebRequest wr=new WebRequest(new Callback() {
@Override
public void call(String result) {
((EditText)view.findViewById(R.id.editText)).setText(result);
}
});
wr.execute("http://www.cheesejedi.com/rest_services/get_big_cheese?level=1");
}
});
选项 2:使用库
但是,如果您确实想使用库,我推荐我的droidQuery图书馆。使用这个,你可以简化你的代码:
Button btn = (Button)findViewById(R.id.myshowbutton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WebRequest wr=new WebRequest(view.getRootView());
wr.execute("http://www.cheesejedi.com/rest_services/get_big_cheese?level=1");
}
});
到:
$.with(this, R.id.myshowbutton).click(new Function() {
@Override
public void invoke($ d, Object... args) {
$.ajax(new AjaxOptions().url("http://www.cheesejedi.com/rest_services/get_big_cheese?level=1").success(new Function() {
@Override
public void invoke($ d, Object... args) {
$.with(MyActivity.this, R.id.editText).text(args[0].toString());
}
}));
}
});
并且您不再需要您的 WebRequest
类。
关于Android异步rest web请求设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974918/