gpt4 book ai didi

java - 从 API 获取数据

转载 作者:行者123 更新时间:2023-11-30 01:41:59 25 4
gpt4 key购买 nike

<分区>

我想在 Android 应用程序中从我的 PHP 服务器获取一些数据。我编写了以下类来执行此操作:

package com.example.sampledomain.myapplication;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Api {

private String targetURL;

private String parameters;

public Api(String targetURL, String parameters)
{
this.targetURL = targetURL;
this.parameters = parameters;
}

public String execute()
{
HttpURLConnection connection = null;
try {
URL url = new URL(this.targetURL);
connection = this.createPostConnection(url);
this.sendRequest(connection);
String response = this.getResponse(connection);
return response;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

private HttpURLConnection createPostConnection(URL url)
{
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer.toString(this.parameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
return connection;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

private void sendRequest(HttpURLConnection connection)
{
try {
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(this.parameters);
wr.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private String getResponse(HttpURLConnection connection)
{
try {
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

}

但是当我调用它时:

String url = "http://sampledomain.pl/test.php";
String params = "";
Api ApiObj = new Api(url, params);
String response = ApiObj.execute();
System.out.print(response);

我不断收到巨大的错误执行堆栈:

12-19 13:52:31.258 2329-2329/com.example.sampledomain.myapplication W/System.err: android.os.NetworkOnMainThreadException
12-19 13:52:31.258 2329-2329/com.example.sampledomain.myapplication W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
12-19 13:52:31.258 2329-2329/com.example.sampledomain.myapplication W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
12-19 13:52:31.258 2329-2329/com.example.sampledomain.myapplication W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
12-19 13:52:31.258 2329-2329/com.example.sampledomain.myapplication W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
12-19 13:52:31.258 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
12-19 13:52:31.258 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:245)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.example.sampledomain.myapplication.Api.sendRequest(Api.java:62)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.example.sampledomain.myapplication.Api.execute(Api.java:28)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.example.sampledomain.myapplication.MainActivity.showText(MainActivity.java:39)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at android.view.View.performClick(View.java:5198)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at android.view.View$PerformClick.run(View.java:21147)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at android.os.Looper.loop(Looper.java:148)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-19 13:52:31.259 2329-2329/com.example.sampledomain.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

怎么了?看起来我可以从字符串中创建一个 URL 对象,但它在创建 header 时抛出错误 - 当我发送 header 时它在某处中断。

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