Android上的HTTP GET和POST失败

时间:2014-11-15 21:53:20

标签: java android http-post http-get

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

这个程序在我的桌面PC上运行得很好但是当我将代码移动到android活动并尝试在那里使用它时,它每次都会失败。关于为什么会这样的任何想法?是否有理由说明HTTP Post或者在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();

用户连接是&#34;&#34; Mozilla / 5.0&#34;&#34;并且网址为&#34; https://accounts.google.com/ServiceLoginAuth&#34;

此外,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.

1 个答案:

答案 0 :(得分:1)

我发现您获得NetworkOnMainThreadException这基本上意味着您应该在单独的线程上执行请求。