ArrayIndexOutOfBound在加载互联网图片期间



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

解决方法:我通过向数组添加额外的长度来使其工作。这有效,但我担心额外的长度会导致位图工厂产生不需要的结果。

谁能告诉我解决这个问题的方法?还是我应该始终添加一些安全性并将文件的确切长度传递给位图工厂?

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

   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字符串包含结果,您可以根据需要在项目中使用它

最新更新