HttpUrlConnection在Jelly Bean中不起作用

时间:2017-03-20 11:08:34

标签: android httpurlconnection android-4.3-jelly-bean

我遇到了有关HttpUrlConnection的问题。除了JellyBean之外,它在每个操作系统中都能完美运行,我不知道为什么。我有2个模拟器彼此相邻,一个运行Nougat,另一个运行JellyBean API 18,在Nougat中请求通过并且在JellyBean中它没有

这是我的代码:

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(60000);
conn.setConnectTimeout(1000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(true);
OutputStream os = conn.getOutputStream(); <-- Gets stuck here and times out after the 60 seconds I gave in the readtimeout
BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(paramList));
writer.flush();
writer.close();
os.close();

conn.connect();

BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result2 = bis.read();
while(result2 != -1) {
    buf.write((byte) result2);
    result2 = bis.read();
}
result = buf.toString();

抛出的错误是UnknownHostException:没有与主机名关联的地址,但我认为它与超时到期时无关。

感谢任何帮助。

0 个答案:

没有答案