我试图用HTTPoison编写一个网页抓取工具。作为第一步,我沿着下面的步骤写了一个简短的HTTP访问代码;Elixir:如何正确描述mix.exs设置?
通过混合
创建一个项目$混用新httptest1
撰写的lib/httptest1.ex短代码。
defmodule Httptest1 do require HTTPoison def test1 do ret = HTTPoison.get! "http://www.yahoo.com" %HTTPoison.Response{status_code: 200, body: body} = ret IO.inspect body end end Httptest1.test1()
修改mix.exs为HTTPoison。
defmodule Httptest1.Mixfile do use Mix.Project def project do [app: :httptest1, version: "0.0.1", elixir: "~> 1.0", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps] end # Configuration for the OTP application def application do [applications: [:logger, :httpoison]] end # Dependencies can be Hex packages: # defp deps do [ {:httpoison, "~> 0.6"} ] end end
运行
$ mix deps.get
的依赖关系。运行
$ mix run
,则编译失败;==> idna (compile) Compiled src/idna.erl Compiled src/idna_ucs.erl Compiled src/punycode.erl (... snip ...) Generated httpoison app == Compilation error on file lib/httptest1.ex == ** (exit) exited in: :gen_server.call(:hackney_manager, {:new_request, #PID<0.154.0>, #Reference<0.0.1.1724>, {:client, :undefined, :hackney_dummy_metrics, :hackney_tcp_transport, 'www.yahoo.com', 80, "www.yahoo.com", [connect_timeout: 5000, recv_timeout: :infinity], nil, nil, nil, true, :hackney_pool, :infinity, false, 5, false, 5, nil, nil, nil, :undefined, :start, nil, :normal, false, false, false, false, nil, :waiting, nil, 4096, "", [], :undefined, nil, nil, nil, nil, :undefined, nil}}, :infinity) ** (EXIT) no process (stdlib) gen_server.erl:212: :gen_server.call/3 src/hackney_client/hackney_manager.erl:66: :hackney_manager.init_request/1 src/hackney_client/hackney_manager.erl:56: :hackney_manager.new_request/1 src/hackney_connect/hackney_connect.erl:181: :hackney_connect.socket_from_pool/4 src/hackney_connect/hackney_connect.erl:36: :hackney_connect.connect/5 src/hackney_client/hackney.erl:319: :hackney.request/5 lib/httpoison.ex:60: HTTPoison.request/5 lib/httpoison.ex:60: HTTPoison.request!/5
当我使用$ iex -S mix
代替,其结果是一样的。
但是,如果我将httptest1.ex移动到放置mix.exs的相同目录中,如$ mv lib/httptest1.ex .
,并试图明确指定源文件;它工作正常。
问题: 我怀疑我的mix.exs设置出了问题,那是什么?
我有类似的错误信息。该代码在IEX中正确执行且没有错误,但在运行混合任务时会引发错误。为我工作的解决方案是“HTTPoison.start”也许这有助于在另一种情况下出现类似错误消息的人。 –