How to Http Request Clojure on Android [lein droid

2019-07-11 09:46发布

问题:

I am using lein droid plugin for developing android app. but i have a problem about receiving any data from internet.

[EDIT] this is just opened app and click the button that contain slurp.

I/ActivityManager(  352): Displayed bnm.ikinci.app/.MainActivity: +574ms
D/AndroidRuntime( 1134): Shutting down VM
W/dalvikvm( 1134): threadid=1: thread exiting with uncaught exception (group=0xa5ab4ba8)
E/AndroidRuntime( 1134): FATAL EXCEPTION: main
E/AndroidRuntime( 1134): Process: bnm.ikinci.app, PID: 1134
E/AndroidRuntime( 1134): clojure.lang.ArityException: Wrong number of args (1) passed to: main/MainActivity-onCreate/fn--49
E/AndroidRuntime( 1134):    at clojure.lang.AFn.throwArity(AFn.java:429)
E/AndroidRuntime( 1134):    at clojure.lang.AFn.invoke(AFn.java:32)
E/AndroidRuntime( 1134):    at neko.listeners.view$on_click_call$reify__174.onClick(view.clj:26)
E/AndroidRuntime( 1134):    at android.view.View.performClick(View.java:4438)
E/AndroidRuntime( 1134):    at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime( 1134):    at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime( 1134):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 1134):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime( 1134):    at android.app.ActivityThread.main(ActivityThread.java:5001)
E/AndroidRuntime( 1134):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1134):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 1134):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime( 1134):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime( 1134):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  352):   Force finishing activity bnm.ikinci.app/.MainActivity
D/dalvikvm(  352): GC_FOR_ALLOC freed 853K, 15% free 6436K/7492K, paused 47ms, total 50ms
W/ActivityManager(  352): Activity pause timeout for ActivityRecord{a614d430 u0 bnm.ikinci.app/.MainActivity t7 f}
W/EGL_emulation(  524): eglSurfaceAttrib not implemented

[/EDIT]

I paste these two lines on my AndroidManifest.template.xml on my root directory as true place [under the use-sdk tag];

  <uses-permission android:name="android.permission.INTERNET" /> 
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

After this step i just try get some data from internet (slurp "https://www.google.com") but i bind this statement to a button's on-click event, when i click; i get app has stopped.

Another way,

(defn maslurp [url]
  (let [a (new java.io.BufferedReader
               (new java.io.InputStreamReader
                    (.getInputStream (doto (.openConnection (new java.net.URL url))
                                       (.setRequestMethod "GET")
                                       (.setDoOutput true)
                                       (.setUseCaches false)))))]
    (loop [r (.readLine a) s ""]
      (if (nil? r)
        s
        (recur (.readLine a) (clojure.string/join [s r]))))))


(maslurp "https://www.google.com") ; doesn't work either.

i tried same apk on my phone SM-C101 (S4 Zoom) and my emulator.

by the way, i know apache libraries for http request on android but i couldn't use it on my lien droid project.

[yes i am sure i have internet on my devices]

What have i done wrong ?

sorry for my bad english, thanks for your help from now.

回答1:

The above (maslurp) works in the android Clojure REPL app, if (.setDoOutput) is set to false instead, per this question:

(defn maslurp [url]
  (let [a (new java.io.BufferedReader
               (new java.io.InputStreamReader
                    (.getInputStream (doto (.openConnection (new java.net.URL url))
                                       (.setRequestMethod "GET")
                                       (.setDoOutput false) ; false for GET
                                       (.setUseCaches false)))))]
    (loop [r (.readLine a) s ""]
      (if (nil? r)
        s
        (recur (.readLine a) (clojure.string/join [s r]))))))


(maslurp "https://www.google.com") ; works in Clojure REPL app