ArrayIndexOutOfBound 在加载网络图片时

ArrayIndexOutOfBound during loading internet picture

我正在尝试从我的服务器加载一张图片到我的 android 上。 我让它工作了。但是奇怪的是我的代码中有 ArrayIndexOutOfBoundsException below.Since 我正在使用 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();
    }

没有必要获取长度和所有内容来获取服务器的完整响应,您可以只检查 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字符串包含结果,您可以根据需要在您的项目中使用它