0

http GET を介して REST サービスから apk ファイルをダウンロードしようとしています。受け取る形式は JSON です。データを受信できますが、Java オブジェクトへのデシリアライズ中に失敗します。Outofmemory エラーが発生します。

非同期タスクでファイル ダウンロード プロセスを実行しています。同じ機能を実現するための他のより良いオプションはありますか? apk は約 2 MB です。wcf サービスからのデータは、json でエンコードされた Base64 です。

private  class DownloadFile extends AsyncTask<String, Integer, String>
{
    @Override
    protected String doInBackground(String... sUrl) 
    {
        try
        {
             URL url = new URL(sUrl[0]); 
              HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
              c.setRequestMethod("GET"); 
              c.setRequestProperty("Content-Type", "application/json");
              c.setRequestProperty("Accept", "application/json");
              c.setRequestProperty("Device","13");
              c.setRequestProperty("Authorization","toughsecurity");
              c.setDoInput(true); 
              c.connect();                    
              String PATH = Environment.getExternalStorageDirectory() + "/download/"; 
              File file = new File(PATH); 
              file.mkdirs(); 
              File outputFile = new File(file, "app.apk"); 
              FileOutputStream fos = new FileOutputStream(outputFile);                    
             if(c.getResponseCode()==HttpURLConnection.HTTP_OK)
          {

                OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/"+"temp.txt");

                int read = 0;
                byte[] bytes = new byte[1024];

                while ((read = c.getInputStream().read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }

                out.flush();
                out.close(); 

                InputStream in=new FileInputStream(Environment.getExternalStorageDirectory() + "/download/"+"temp.txt");


               ObjectMapper mapper=new ObjectMapper();

              AppDownload download= mapper.readValue(in, AppDownload.class);

                 byte[] data1=Base64Coder.decodeLines(download.Data);
                  byte[] content = data1;
                int size = content.length;
                InputStream is1 = null;
                byte[] b = new byte[size];           
                is1 = new ByteArrayInputStream(content);
                byte[] buffer = new byte[1024]; 
                 int len1 = 0; 
                 while ((len1 = is1.read(buffer)) != -1) { 
                     fos.write(buffer, 0, len1); 
                 } 
                 fos.close(); 




          }


             c.disconnect(); 


        } 
        catch (Exception e)
        {
            Log.e("Jackson error",e.getMessage());
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
      //  mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
       // mProgressDialog.setProgress(progress[0]);
    }



}
4

1 に答える 1

0

JSON ライブラリが 2MB の JSON ファイルを要求していても驚かないでしょう。

おそらくAPIを変更するオプションではないことはわかっていますが、JSONでバイナリデータを返すのはかなり奇妙です. そのために作られているわけではありません。API は 2 つのエンドポイントを提供する必要があります。1 つはコンテンツへのリンクを含むオブジェクト メタ データを取得し、もう 1 つはコンテンツをバイナリ形式でのみ返します。

別の JSON ライブラリを試すこともできます。

于 2012-08-15T22:50:30.880 に答える