OO Design for using libGDX’s HttpResponseListener

huangapple go评论67阅读模式
英文:

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接口。按照你的使用方式来实现cancelledhandleHttpResponsefailed方法,然后在你的类中添加抽象方法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.

huangapple
  • 本文由 发表于 2020年8月28日 20:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63633552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定