HttpUrlConnection响应意外状态行

时间:2014-05-12 09:38:11

标签: android httpurlconnection cache-control

我正在尝试使用HttpUrlConnection同步连接我的三个不同的Web服务,但是HttpUrlConnection得到了响应。将我之前的响应NotModified添加到当前响应中我该如何解决它。我也尝试使用清除缓存。

    conn.setUseCaches(false);
    conn.setDefaultUseCaches(false);  

但上述方法无效。

代码: -

    public String loadJsonFromNetwork(String urls,ArrayList<BasicNameValuePair> params)throws IOException,NotModifiedException,UnAuthorisedAccess {
    InputStream stream=null;
    try {
        stream = loadUrl(urls,params);
        return getJSON(stream);
        // Makes sure that the InputStream is closed after the app is
        // finished using it.
    } finally {
        if (stream != null) {
            stream.close();
       }
     }
  }

 public InputStream loadUrl(String urlString,ArrayList<BasicNameValuePair> params) throws IOException,NotModifiedException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.addRequestProperty("Cache-Control", "max-age=0");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setAllowUserInteraction(true);
HttpResponseCache.setDefault(null);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8"));
writer.write(utility.getQuery(params));
writer.flush();
writer.close();
os.close();
// Starts the query
conn.connect();
//conn.setRequestProperty();
Log.e("response message", conn.getContentType() + "");

if(conn.getHeaderFields()!=null){
    Log.d("Header",conn.getHeaderFields().toString());
}

if(conn.getResponseCode() != HttpStatus.SC_NOT_MODIFIED)
        return conn.getInputStream();
else
        throw new NotModifiedException(HttpStatus.SC_NOT_MODIFIED+"");
}

logcat: -

java.net.ProtocolException: Unexpected status line: Not ModifiedHTTP/1.1 200 OK
05-12 14:24:47.316  13808-13853/com.xxx.xxxxxx W/System.err﹕ at com.android.okhttp.internal.http.RawHeaders.setStatusLine(RawHeaders.java:108)
05-12 14:24:47.316  13808-13853/com.xxx.xxxxx W/System.err﹕ at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308)
05-12 14:24:47.316  13808-13853/com.xxx.xxxxx W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135)
05-12 14:24:47.316  13808-13853/com.xxx.xxxxx W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644)
05-12 14:24:47.316  13808-13853/com.xxx.xxxx W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
05-12 14:24:47.316  13808-13853/com.xxx.xxxx W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
05-12 14:24:47.316  13808-13853/com.xxx.xxxx W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:143)
05-12 14:24:47.316  13808-13853/com.xxx.xxxx W/System.err﹕ at java.net.URLConnection.getContentType(URLConnection.java:326)
05-12 14:24:47.316  13808-13853/com.xxx.xxxx W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getContentType(HttpsURLConnectionImpl.java:186)
05-12 14:24:47.316  13808-13853/com.xxx.xxxx W/System.err﹕ at com.crosshair.util.utility.loadUrl(utility.java:92)
05-12 14:24:47.316  13808-13853/com.xxx.xxxx W/System.err﹕ at com.crosshair.util.utility.loadJsonFromNetwork(utility.java:50)
05-12 14:24:47.316  13808-13853/com.xxx.xxxxxx W/System.err﹕ at com.xxx.xxxxxx.loginActivity$FleetRequestAsynctask.doInBackground(loginActivity.java:377)
05-12 14:24:47.316  13808-13853/com.xxx.xxxxxx W/System.err﹕ at com.xxx.xxxxxx.loginActivity$FleetRequestAsynctask.doInBackground(loginActivity.java:357)
05-12 14:24:47.326  13808-13853/com.xxx.xxxxxx W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-12 14:24:47.326  13808-13853/com.xxx.xxxxxx W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-12 14:24:47.326  13808-13853/com.xxx.xxxxxx W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-12 14:24:47.326  13808-13853/com.xxx.xxxxxx W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-12 14:24:47.326  13808-13853/com.xxx.xxxxxx W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-12 14:24:47.326  13808-13853/com.xxx.xxxxxx W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

标题: -

{null=[HTTP/1.1 304 Not Modified], Connection=[Keep-Alive], Date=[Mon, 12 May 2014 09:29:56 GMT], Keep-Alive=[timeout=5, max=99], Server=[Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/0.9.7a mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.2.17], X-Android-Received-Millis=[1399886997022], X-Android-Response-Source=[NETWORK 304], X-Android-Selected-Transport=[http/1.1], X-Android-Sent-Millis=[1399886996580]}

并且conn.getHeaderFields()方法返回null,而HttpUrlConnection命中我的第三个Web服务为什么?

这是android os中的bug .... 现在我正在使用android 4.4.2 nexus 7

0 个答案:

没有答案