英文:
OO Design for using libGDX's HttpResponseListener
问题
我有许多不同的 HTTP 调用需要进行,为每个调用构建了自定义的监听器。这些监听器都是相同的,除了在 handleHttpResponse 中可能会有一两个方法调用不同。因此,大部分以下内容每次都会被重写:
HttpResponseListener listener = new HttpResponseListener() {
String status;
public void handleHttpResponse(HttpResponse httpResponse) {
getSession(httpResponse);
String result = httpResponse.getResultAsString();
if (result != null) {
getNeeds(result); // <== 独特的部分
}
System.out.println("网络响应:成功");
}
public void failed(Throwable t) {
status = "fail";
System.out.println("网络响应:[失败] " + status);
t.printStackTrace();
}
@Override
public void cancelled() {
status = "cancelled";
System.out.println("[getNeedType()] 网络响应:[取消] " + status);
}
};
如何从所有这些重复的复制粘贴中清理出我的代码呢?在 Java 文档中提到:
> 将数据传递给渲染线程应该使用 {@link Application#postRunnable(java.lang.Runnable runnable)}
虽然之前没有使用过 Runnable,但我认为它们是要执行的自定义逻辑块。尽管有“post”前缀,但我不知道如何将一个 Runnable 附加到监听器执行的末尾。我考虑扩展 HttpResponseListener 以接受一个 Runnable 作为参数,然后在 handleHttpResponse 中获取结果后调用该 Runnable 块。但是如何将结果字符串传递给 Runnable 呢?Runnable 似乎不接受任何参数。是否还需要扩展可接受参数的 Runnable 呢?
英文:
I have lots of different http calls to make, and have been constructing custom listeners for each one. The listeners are all the same, except for a method call or two within the handleHttpResponse. So most of the following gets rewritten each and every time:
HttpResponseListener listener = new HttpResponseListener() {
String status;
public void handleHttpResponse(HttpResponse httpResponse) {
getSession(httpResponse);
String result = httpResponse.getResultAsString();
if (result != null) {
getNeeds(result); // <== unique part
}
System.out.println("Network Response:: Success");
}
public void failed(Throwable t) {
status = "fail";
System.out.println("Network Response:: [Fail] " + status);
t.printStackTrace();
}
@Override
public void cancelled() {
status = "cancelled";
System.out.println("[getNeedType()] Network Response:: [Cancelled] " + status);
}
};
How do I de-clutter my code from all these repetitive re-pastes? Within the javadoc it says to:
> Passing data to the * rendering thread should be done using {@link
> Application#postRunnable(java.lang.Runnable runnable)}
Haven't used runnables before, but I think they're custom blocks of logic to be executed. Despite the 'post' prefix I can't see how to attach a runnable to the end of the listeners execution. I'm thinking to extend the HttpResponseListener to accept a Runnable as an argument, then call the Runnable block at the point in handleHttpResponse when I get my results back. But how do I pass the results string to the runnable? Runnables don't see to take any arguments. Do you extend the runnable as well to do so?
答案1
得分: 1
你创建一个新的抽象类,实现HttpResponseListener
接口。按照你的使用方式来实现cancelled
、handleHttpResponse
和failed
方法,然后在你的类中添加抽象方法getNeeds
。
从现在开始,使用你的新抽象类。
英文:
You make a new abstract class implementing HttpResponseListener
. Implement cancelled, handleHttpResponse and failed methods the way you use, add the abstract method getNeeds
to your class.
From now on, use your new abstract class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论