我已经在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