gpt4 book ai didi

java - Android 上的 HTTP GET 和 POST 失败

转载 作者:行者123 更新时间:2023-11-30 02:33:36 24 4
gpt4 key购买 nike

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

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

是的 - 我的 list 中有以下权限:

<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.

最佳答案

我看到你得到一个 NetworkOnMainThreadException这基本上意味着您应该在单独的线程上执行您的请求。

关于java - Android 上的 HTTP GET 和 POST 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26951144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com