我已經在amazon ec2實例中創建了我的服務器。通過我的Android應用程序,我使用HttpUrlConnection連接到服務器。但我得到的響應代碼爲-1。有沒有人有任何想法?這是我的代碼。 私人字符串getHttpResponse(最後絃樂srcUrl){HttpUrlConnection響應代碼總是返回-1
HttpURLConnection urlConnection = null;
FileOutputStream fos = null;
try {
URL url = new URL(srcUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Content-Type", "application/json");
mETag = readETagFromPrefForCategory();
urlConnection.setRequestProperty("If-None-Match", mETag);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xyz", "abc".toCharArray());
}
});
urlConnection.connect();
mETag = urlConnection.getHeaderField("ETag");
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
Log.v("http","not modifed");
return readLocalJson();
}
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.w(TAG, "Bad response [" + urlConnection.getResponseCode() + "] from (" + srcUrl + ")");
return null;
}}//end of try block
感謝您的回覆..我試過System.setProperty(「http.keepAlive」,「false」);但可悲的是它並沒有爲我工作:(..其他任何幫助表示讚賞.. – user1479604