小编G-J*_*G-J的帖子

'readFile'的异常处理

我正在尝试为函数添加一个简单的处理程序readFile:

readHandler :: IOError -> IO ()  
readHandler e  
    | isDoesNotExistError e = putStrLn "The file does not exist"
    | otherwise = putStrLn "Something went wrong"  

main = do
    // stop executing if there is no file   
    contents <- (readFile "path.xml") `catch` readHandler
    // generates [(x,y),(x,y)]
    coordinates = parseXML contents
    // I want to do something with the coordinates
    nextFunction coordinates
Run Code Online (Sandbox Code Playgroud)

当我尝试编译这个时,我收到错误:

Couldn't match type `()' with `[Char]'
Expected type: IOError -> IO String
  Actual type: IOError -> …
Run Code Online (Sandbox Code Playgroud)

error-handling file-io haskell handler

4
推荐指数
1
解决办法
3672
查看次数

改造 2 错误:NetworkOnMainThreadException

我正在尝试对 Android 中的服务器执行同步请求。(代码在普通 Java 项目中完美运行)

我知道同步请求不应该在主线程上完成,所以我启动了一个新线程来完成网络连接。我也知道可以进行异步调用,但同步调用更适合我的用例。

bool networkingIsDoneHere(){
  dostuff();
  int number = doNetworkCall();
  if(number < 500){
    return true;
  }
  return false
}
Run Code Online (Sandbox Code Playgroud)

问题是我仍然收到“NetworkOnMainThreadException”错误。改造是否以某种方式在主线程上运行,同时在侧线程上执行?

开始新线程:

Thread timerThread = new Thread() {
        @Override
        public void run() {
            while (true) {
                try {
                    networkingIsDoneHere();
                    sleep(getExecutionInterval());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };

    @Override
    public void startRule() {
        timerThread.start();
    }
Run Code Online (Sandbox Code Playgroud)

改造代码

    private Retrofit createRetrofitAdapter() throws Exception {
        return retrofit = new Retrofit.Builder()
                .baseUrl("https://maps.googleapis.com/maps/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }

    public interface ApiGoogleMapsService {
        @GET("url...") …
Run Code Online (Sandbox Code Playgroud)

multithreading android retrofit retrofit2

4
推荐指数
1
解决办法
8176
查看次数

如何(安装和)在 haskell 中使用包

我正在尝试在 ghci 中使用这个 xml 包。

我已经在 cmd 中做了什么:

cabal 安装 cabal-install

阴谋集团安装 xml-1.3.13.tar.gz

并且安装成功。但是我如何在我的脚本和 GHCi 中使用这个包?我正要把我的电脑扔出窗外自动取款机...

haskell package

3
推荐指数
1
解决办法
1728
查看次数

带有自定义注释的 Spring bean

我已经用自定义注释注释了 Spring bean,但似乎 Spring 在创建 bean 后删除了我的自定义注释。

AnnotatedBean bean = ctx.getBean(AnnotatedBean.class);

Foo.findAndDoStuffWithAnnotatedThings(bean);
Run Code Online (Sandbox Code Playgroud)

第二步不起作用,我的自定义注释丢失了。(可能是由于代理的东西)

我的豆子

@Rule(name = "RoutePickupRule")
@Transactional
@Component
public class AnnotatedBean{


    @Autowired
    private ICarpoolDoa carpoolDAO;

    @Condition
    public boolean condition(CustomLocation customLocation, String userId) {
        //snip
    }

    @Action
    public void action() {
        //snip
    }  
Run Code Online (Sandbox Code Playgroud)

我的自定义注释之一的示例

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Condition {

}
Run Code Online (Sandbox Code Playgroud)

findAndDoStuffWithAnnotatedThings Bean中的错误
被传递到验证我的自定义注释的类,但我的验证程序找不到任何注释。(Util 使用isAnnotationPresent方法)。同样,当我使用“new”自己创建 bean 时,没有问题。

public class RuleAnnotationVerifier {


    public void RuleAnnotationVerifier(final Object rule) {
        checkRuleClass(rule);
        checkConditionMethod(rule);
        checkActionMethod(rule);
    }

    private void checkRuleClass(final Object rule) {
        if …
Run Code Online (Sandbox Code Playgroud)

spring annotations spring-bean

1
推荐指数
1
解决办法
4886
查看次数