【发布时间】:2025-12-23 16:05:11
【问题描述】:
当我运行我的 android 应用程序时,我得到一个 响应代码 = 400,但是当我在浏览器和邮递员中测试相同的 URL 时,它的响应代码为 200。我做错了什么。这是get Request方法
public static String getRequest(String myUrl) throws IOException {
InputStream is = null;
try {
URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(30000 /*milliseconds*/);
conn.setConnectTimeout(20000 /*milliseconds*/);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
if (response != HttpURLConnection.HTTP_OK)
is = conn.getErrorStream();
else
is = conn.getInputStream();
Log.i(TAG, String.valueOf(response));
// Convert the InputStream into a string
return readStream(is);
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
我需要帮助。
【问题讨论】:
-
你使用本地服务?
-
不,我正在使用在线服务器
-
错误 400 它的错误请求,检查你的值
-
您的 url 中是否有任何 Queryparam 或邮递员请求中是否有特殊的 HTTP 标头
-
代码是否适用于其他 URL?