gpt4 book ai didi

android - 使用 HTTPUrlConnection 进行对解析服务器的 REST API 调用

转载 作者:行者123 更新时间:2023-11-29 00:07:35 25 4
gpt4 key购买 nike

我正在尝试使用 HTTPURLConnection 对 Parse.com 服务器进行 RESTful API 调用。不幸的是,当我运行下面的代码时,出现 HTTP 401(未授权访问)错误

代码如下

protected JSONObject doInBackground(String... params) {
HttpsURLConnection urlConnection = null;
try {
URL url = new URL("https://api.parse.com/1/functions/hello");
urlConnection =
(HttpsURLConnection) url.openConnection();

urlConnection.setConnectTimeout(20000);
urlConnection.setReadTimeout(20000);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("X-Parse-Application-Id", appId);
urlConnection.setRequestProperty("X-Parse-REST-API-Key", restApiKey);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.connect();

byte[] outputBytes = "{}".getBytes("UTF-8");
OutputStream out = urlConnection.getOutputStream();
out.write(outputBytes);
out.close();

int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpURLConnection.HTTP_ACCEPTED) {
Log.d(TAG, "doInBackground(): connection failed: statusCode: " + statusCode);
// return null;
}

InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
String responseText = getResponseText(in);
Log.d(TAG, "doInBackground() responseText: " + responseText);
return new JSONObject(responseText);

} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;

基本上,我只需要复制这个 cURL 的功能:

curl -X POST \
-H "X-Parse-Application-Id: {APP_ID}" \
-H "X-Parse-REST-API-Key: {API_KEY}" \
-H "Content-Type: application/json" \
-d '{}' \
https://api.parse.com/1/functions/hello

我使用相同的 App ID 和 API key 运行了完全相同的 cURL 命令,它们返回了正确的结果。

有什么建议吗??

编辑:

这是我得到的控制台输出:

09-24 14:28:43.101  21868-22057/com.example.versiontrack D/Module_VersionTracker﹕ doInBackground(): connection failed: statusCode: 401
09-24 14:28:43.102 21868-22057/com.example.versiontrack W/System.err﹕ java.io.FileNotFoundException: https://api.parse.com/1/functions/hello
09-24 14:28:43.102 21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
09-24 14:28:43.103 21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
09-24 14:28:43.103 21868-22057/com.example.versiontrack W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at com.example.module_versiontracker.Module_VersionTracker$ParseVersionCheckTask.doInBackground(Module_VersionTracker.java:215)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at com.example.module_versiontracker.Module_VersionTracker$ParseVersionCheckTask.doInBackground(Module_VersionTracker.java:115)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-24 14:28:43.104 21868-22057/com.example.versiontrack W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-24 14:28:43.105 21868-22057/com.example.versiontrack W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

最佳答案

我想你忘了刷新输出流。

DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.write(outputBytes);
out.flush(); //Don't forgot to flush the output
out.close();

关于android - 使用 HTTPUrlConnection 进行对解析服务器的 REST API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32753175/

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