【问题标题】:HTTP GET and POST failing on AndroidAndroid 上的 HTTP GET 和 POST 失败
【发布时间】:2014-11-15 21:53:20
【问题描述】:

我正在构建一个 Android 应用程序,它使用 HTTP Get 和 HTTP Post 与 googles Service Auth 通信,以验证我的 google 凭据并返回结果。

这个程序在我的台式电脑上运行良好,但是当我将代码移动到一个 android 活动并尝试在那里使用它时,它每次都失败。关于为什么会这样的任何想法? HTTP Post 或 get 在 Android 应用中不起作用是否有原因?

是的 - 我的清单中有以下权限:

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

简而言之,我的代码如下:

    URL obj = new URL(url);
    conn = (HttpsURLConnection) obj.openConnection();

    // default is GET
    conn.setRequestMethod("GET");

    conn.setUseCaches(false);

    // act like a browser
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept",
            "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    if (cookies != null) {
        for (String cookie : this.cookies) {
            conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
        }
    }
    int responseCode = conn.getResponseCode();
    /* System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode); */

    BufferedReader in = 
            new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    // Get the response cookies
    setCookies(conn.getHeaderFields().get("Set-Cookie"));

    return response.toString();

用户连接是“Mozilla/5.0”,URL 是“https://accounts.google.com/ServiceLoginAuth

另外,Logcat 不断抛出以下错误:

11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at libcore.io.Posix.open(Native Method)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.io.File.createNewFile(File.java:939)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ ... 17 more
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/Download/log.file: open failed: EACCES (Permission denied)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.io.FileWriter.<init>(FileWriter.java:58)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at mattmcgrath.me.locationhistoryapp.ConnectToLocationHistory.appendLog(ConnectToLocationHistory.java:56)
11-15 22:10:04.595  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at mattmcgrath.me.locationhistoryapp.ConnectToLocationHistory.ConnectToGoogle(ConnectToLocationHistory.java:70)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at mattmcgrath.me.locationhistoryapp.MyActivity.login(MyActivity.java:41)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.view.View$1.onClick(View.java:3825)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.view.View.performClick(View.java:4445)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.view.View$PerformClick.run(View.java:18446)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5139)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
11-15 22:10:04.600  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
11-15 22:10:04.605  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
11-15 22:10:04.605  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
11-15 22:10:04.605  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at libcore.io.Posix.open(Native Method)
11-15 22:10:04.605  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-15 22:10:04.605  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:393)
11-15 22:10:04.605  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ ... 19 more
11-15 22:10:05.220  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ android.os.NetworkOnMainThreadException
11-15 22:10:05.225  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
11-15 22:10:05.225  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
11-15 22:10:05.230  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at mattmcgrath.me.locationhistoryapp.ConnectToLocationHistory.GetPageContent(ConnectToLocationHistory.java:164)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at mattmcgrath.me.locationhistoryapp.ConnectToLocationHistory.ConnectToGoogle(ConnectToLocationHistory.java:82)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at mattmcgrath.me.locationhistoryapp.MyActivity.login(MyActivity.java:41)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.view.View$1.onClick(View.java:3825)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.view.View.performClick(View.java:4445)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.view.View$PerformClick.run(View.java:18446)
11-15 22:10:05.235  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
11-15 22:10:05.240  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
11-15 22:10:05.240  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
11-15 22:10:05.240  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5139)
11-15 22:10:05.240  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
11-15 22:10:05.240  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
11-15 22:10:05.240  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
11-15 22:10:05.240  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
11-15 22:10:05.240  24998-24998/mattmcgrath.me.locationhistoryapp W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
11-15 22:10:05.250  24998-24998/mattmcgrath.me.locationhistoryapp I/Choreographer﹕ Skipped 43 frames!  The application may be doing too much work on its main thread.

【问题讨论】:

  • 您是否正在向清单添加权限?如果答案是肯定的,请发布您的代码!
  • 已发布代码并包含 logcat
  • 是的,设法解决了这个问题,新的 logcat 信息发布在上面

标签: java android http-post http-get


【解决方案1】:

我看到您收到了NetworkOnMainThreadException,这基本上意味着您应该在单独的线程上执行您的请求。

【讨论】:

    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多