2014-11-15 3 views
1

Я создаю приложение для Android, которое связывается с googles Service Auth, используя HTTP Get и HTTP Post для аутентификации моих учетных данных google и возврата результата.HTTP GET и POST сбой на Android

Эта программа отлично работает на моем настольном ПК, но когда я перемещаю код в андроид и пытаюсь использовать его там, он терпит неудачу каждый раз. Любые идеи о том, почему это может быть? Есть ли причина, по которой HTTP-сообщение или получение не будут работать в приложении для 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. 
+1

Вы добавляете разрешения на манифест? Если да, то отправьте свой код! –

+0

опубликуйте свой логарифм – user2717954

+0

Выведенный код And logcat включен – MattTheHack

ответ

1

Я вижу, что вы получаете NetworkOnMainThreadException, что в основном означает, что вы должны выполнять свои запросы в отдельном потоке.