【问题标题】:Intercept Blobstore Request and Save responded File拦截 Blob 存储请求并保存响应文件
【发布时间】:2012-12-13 15:05:47
【问题描述】:

我想在我的 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;
    }
}

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

【问题讨论】:

    标签: android google-app-engine gwt


    【解决方案1】:

    我自己找到了解决办法,想分享给大家:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 2015-09-16
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      相关资源
      最近更新 更多