1

I'm am building an Android application that communications with googles Service Auth using HTTP Get and HTTP Post to authenticate my google credentials and return a result.

This program works perfectly on my Desktop PC but when I move the code to an android activity and try to use it there, it fails everytime. Any ideas on why this could be? Is there a reason why a HTTP Post or get won't work in an Android app?

Yes - I have the following permission in my manifest :

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

My code in a nutshell is as follows :

    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();

Where the user connection is ""Mozilla/5.0"" and the URL is "https://accounts.google.com/ServiceLoginAuth"

Also, Logcat keeps throwing the following error :

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.
MattTheHack
  • 1,354
  • 7
  • 28
  • 48

1 Answers1

1

I see that you're getting a NetworkOnMainThreadException which basically means that you should execute your requests on a separate thread.

Roberto Betancourt
  • 2,375
  • 3
  • 27
  • 35