0

私のサーバーからアンドロイドに画像をロードしようとしています。 私はそれを働かせました。しかし、不思議なことに、ArrayIndexOutOfBoundsExceptionが私のコードの下にあります。私はHttpURLconnection.getContentLength()を使ってバイト配列を生成しています。なぜこのようなことが起こったのか分かりません。 getContentLengthの値は私のサーバー上の正確なファイル長と一致していたので、送信時の問題ではありません。ArrayIndexOutOfBoundインターネットピクチャのロード中

回避策として、配列に余分な長さを追加することで機能するようになりました。これは動作しますが、余分な長さが原因で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(); 
    } 
+0

必要に応じてresponse文字列が結果が含まれている

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(); } 

を、あなたのプロジェクトでそれを使用することができますサーバー – warl0ck

+0

PicassoまたはGlideライブラリを使用してイメージを読み込むことができます。 – Dhiren

+0

@ warl0ck私が投稿したコードはテスト用の電話機で実行され、画像が読み込まれます。だから、そのようなものではない404か何か。なぜ、それが避けられるのかを知りたかっただけです。 – HarryTheF

答えて

0

それは代わりに、あなただけのif the response code is 200 or notこれをチェックすることができ、サーバーから完全な応答を得るために、長さと、すべてを取得する必要はありませんトリックを行う必要があります。あなたが何かを行うことができ、サーバーの応答からの読み取りに

:あなたから得ている応答の種類

関連する問題