使用带 https 地址的 HttpURLConnection 的 Android ICS 无法执行发布方法

标签 android post android-4.0-ice-cream-sandwich httpurlconnection

我从 API lvl 4 开始使用这段代码,它一直有效,直到 ICS。 从 API 级别 14 开始,当我使用 https URL 时,此代码块始终执行 GET 方法而不是 POST。如果我使用 http URL,代码会执行 POST,一切正常。 我从 lvl14 开始得到的异常是 FileNotFoundException。我根本不明白发生了什么。请帮忙。谢谢。

private byte[] Post(byte[] Header, byte[] Body, String protocol) throws IOException, MyAppConnectionException
{
    HttpURLConnection urlConnection = null;
    byte[] responseData = null;

    try
    {
        String url = MyApp.getContext().getResources().getString(R.string.ServerEndPoint);
        URL u = new URL(url);
        urlConnection = (HttpURLConnection) u.openConnection();
        urlConnection.setConnectTimeout(15000);
        urlConnection.setReadTimeout(45000);
        urlConnection.setRequestProperty("CONTENT-TYPE", protocol);
        urlConnection.setDoOutput(true); 
        urlConnection.setDoInput(true);
        urlConnection.setChunkedStreamingMode(0);
        urlConnection.connect();
    }
    catch(IOException ioex)
    {
        throw new MyAppConnectionException();
    }

    if(urlConnection != null)
    {
        DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream());

        int msgLength = (int)(4 + Header.length + Body.length);

        outputStream.write(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(msgLength).array());
        outputStream.write(Header);
        outputStream.write(Body);
        outputStream.flush();
        outputStream.close();

        if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            DataInputStream inputStream = new DataInputStream(urlConnection.getInputStream());

            int dataLength = inputStream.available();
            byte[] msgbLength = new byte[4];
            inputStream.read(msgbLength, 0, 4);
            int length = ByteBuffer.wrap(msgbLength).order(ByteOrder.LITTLE_ENDIAN).getInt();

            assert(dataLength == length);      

            responseData = new byte[length - 4];
            inputStream.readFully(responseData, 0, length - 4);
            inputStream.close();
        }
        urlConnection.disconnect();
    }

    return responseData;}

最佳答案

关于使用带 https 地址的 HttpURLConnection 的 Android ICS 无法执行发布方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10272009/

相关文章:

php - 如何在 foreach 循环中的隐藏字段中分配唯一值

android - 如何在 Android 4.0/Ice Cream Sandwich 中获取该平台?

xml - 使用 Android 生成的 ant 构建文件时如何为 JAR 指定 lib 文件夹?

java - 在 java 和 JSON 中编码希伯来语

android - 通过 USB 从 Arduino 接收数据到 Android 设备

php - 清理 $_POST 变量

Python OAuth WooCommerce

android - ICS 中的广播接收器

android - ICS 中的小部件尺寸

Android MVVM UI 控件