【发布时间】:2016-04-18 00:30:13
【问题描述】:
我正在尝试访问 Web 服务。它适用于 android 4.4 或 android 5.X。但是当我尝试使用 android 4.1.1 来点击“http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID"”时,它总是返回 307 状态码。但是这个 url 在 android 4.4 或 5.x 上运行良好。我也尝试点击其他 url 它工作正常安卓 4.1.1。
所以请告诉我是什么问题
Log.i(TAG, url);
String response = null;
HttpURLConnection conn = null;
try {
URL webServiceUrl = new URL(url);
conn = (HttpURLConnection) webServiceUrl
.openConnection();
Log.i(TAG, "Connection open");
conn.setRequestMethod(GET);
conn.setConnectTimeout(CONNECTION_TIME_OUT);
conn.setRequestProperty(CONTENT_TYPE, contentType);
conn.setRequestProperty(ACCEPT_TYPE, acceptType);
conn.setDoInput(true);
conn.connect();
Log.i(TAG, "Connection Connected");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK && conn.getInputStream() != null) {
response = StreamUtility.convertStreamToString(conn.getInputStream());
conn.getInputStream().close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return response;
}
【问题讨论】:
-
发生这种情况是因为标头在这里检查您的代码一次:- conn.setRequestProperty(TOKEN, header);确保您传递给在 API 客户端中确认的 Web 服务
-
我没有在这里传递任何标题
-
老兄,“conn”的所有设置操作都被视为标题。首先,在 REST API 客户端中检查您的网络服务,然后实现代码。
标签: android httpurlconnection httpconnection