拦截Blobstore请求并保存已响应的文件

时间:2012-12-28 10:17:28

标签: android google-app-engine gwt

我想在我的Android应用程序上拦截对blobservice的请求

到url看起来像这样: http://foo.appspot.com/simpleams/blobservice?blob-key=AMIfv94NAAoxn1oi_ySWYSiNF3MforFVI6SvDi_NeF0rjNr_QW

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        if(url.contains("blobservice")) {
            return getAppWebResourceResponseFromBlobstore(view, url);
        } else {
            return super.shouldInterceptRequest(view, url);
        }

    }

private WebResourceResponse getAppWebResourceResponseFromBlobstore(WebView view, String url) {
    try {
        // TODO: 
                    return file from local data or download it from 
                    blob service, save it and return it...
    } catch (IOException e) {
        return null;
    }
}

如何向服务器发出请求并在本地保存文件?

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案并希望与您分享:

    private WebResourceResponse getAppWebResourceResponseFromBlobstore(WebView view, String url) {


        int slashIndex = url.lastIndexOf("blobservice?blob-key=");

        String key;
        key = url.substring(slashIndex + "blobservice?blob-key=".length());

        System.out.println(key);    
                BlobstoreManager m = new BlobstoreManager(ctx, url);


        return m.getBlob(key);


    }


public class BlobstoreManager{


    Context mContext;
    String url;

    public BlobstoreManager(Context c, String url) {

        this.url = url;
        this.mContext = c;


    }


    public WebResourceResponse getBlob(String key) {

        String path =  key;
        File file = mContext.getFileStreamPath(key);
        if(file.exists()) {
             try {
                 System.out.println("Response from Blob");
                return new WebResourceResponse("", "",mContext.openFileInput(path));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else {
            try {

                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(new HttpGet(url));
                StatusLine statusLine = response.getStatusLine();
                if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                    FileOutputStream output = mContext.openFileOutput(path, Context.MODE_PRIVATE);

                    response.getEntity().writeTo(output);

                    output.flush();
                    output.close();

                } else{
                    //Closes the connection.
                    response.getEntity().getContent().close();
                    throw new IOException(statusLine.getReasonPhrase());
                }


             System.out.println("Save new Blob");
                return new WebResourceResponse("", "",mContext.openFileInput(path));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

}