ArrayIndexOutOfBound在加载互联网图片期间

时间:2017-06-16 06:19:24

标签: java android inputstream httpurlconnection indexoutofboundsexception

我正在尝试从我的服务器上加载我的android上的图片。 我得到了它的工作。但奇怪的是我在下面的代码中得到ArrayIndexOutOfBoundsException。因为我使用HttpURLconnection.getContentLength()来生成我的字节数组,所以我无法弄清楚为什么会发生这种情况。来自getContentLength的值与我服务器上的确切文件长度相匹配,因此它不是传输中的问题。

作为一种解决方法:我通过为数组添加额外的长度来实现它。这有效,但我担心额外的长度会导致BitmapFactory产生不需要的结果。

有人可以告诉我一个解决方案吗? 或者我应该总是添加一些安全性并将文件的确切长度传递给Bitmapfactory?

以下是我的代码的连接部分出错了:

   try{
        Log.d("connect_server","is connect");
        URL url = new URL("someurl");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.connect();

        OutputStreamWriter osw=new OutputStreamWriter(connection.getOutputStream());
        Log.d("post param",params);
        osw.write(params);
        osw.flush();
        osw.close();
        byte[] buf=new byte[connection.getContentLength()+20];
        InputStream src=connection.getInputStream();


        int total=0;
        int amt=512;
        while(amt!=-1){
            Log.d("isloafing",Integer.toString(total));
            amt=src.read(buf,total,amt);
            total+=amt;
            Log.d("is loaDing",Integer.toString(amt));
        }



        Message msg=hnd.obtainMessage();
        msg.arg1=msgTag;
        msg.obj=buf;
        hnd.sendMessage(msg);

    }catch (IOException e)
    {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

没有必要获取长度以及所有来自服务器的完整响应,而只需检查if the response code is 200 or not这应该可以解决问题。

要从服务器响应中读取,您可以执行以下操作:

private String readFromStream(InputStream inputStream) throws IOException {
    StringBuilder output = new StringBuilder();
    if (inputStream != null) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line = reader.readLine();
        while (line != null) {
            output.append(line);
            line = reader.readLine();
        }
    }
    return output.toString();
}

 try{
        String response = "";
        Log.d("connect_server","is connect");
        URL url = new URL("someurl");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);

        OutputStreamWriter osw=new OutputStreamWriter(connection.getOutputStream());
        Log.d("post param",params);
        osw.write(params);
        osw.flush();
        osw.close();
        connection.connect();
        if (urlConnection.getResponseCode() == 200) {
            inputStream = urlConnection.getInputStream();
            response = readFromStream(inputStream);
        }

        // now response has your server response use it however you want


    }catch (IOException e)
    {
        e.printStackTrace();
    }

response字符串包含结果,您可以根据需要在项目中使用它

相关问题