using blobstore with google cloud endpoint and android

后端 未结 3 709
梦毁少年i
梦毁少年i 2020-12-04 09:16

I am developing an app-engine connected android project using the eclipse plugin. One aspect of the app is to allow user Alpha to send pictures to user Bravo. To do that I h

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 09:49

    Since I tried with many way to do the callback service in the api of endpoint, I abort that aproach. However, I could solve that problem making a parallel servlet to the api endpoint, it only needs define the class server and add it web.xml configuration. Here my solution:

    1 Enpoint Service for get the URL for upload: Then the service coudl be protected with clientId

    @ApiMethod(name = "getUploadURL",  httpMethod = HttpMethod.GET)
        public Debug getUploadURL() { 
            String blobUploadUrl =  blobstoreService.createUploadUrl("/update");
            Debug debug = new Debug(); 
            debug.setData(blobUploadUrl);
            return debug; 
        }
    

    2. Now the Client can call to endpoint for get the upload URL:
    Maybe some like this (for android use you client library enpoint too):

    gapi.client.debugendpoint.getUploadURL().execute(); 
    

    3. The next step is todo a post to url catched in last step: You can do that with a httpClient of android, again, in my case I need upload from a web then I use a form, and onChangeFile() event callback for get the uploadurl (using step 3) then when it response to change the form parameters "action" and "codeId" before that someone decide do click on submit button:

    4 Finally the paralele servlet class:

    @SuppressWarnings("serial")
    public class Update  extends HttpServlet{
    
        public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {    
    
            String userId   = req.getParameter("codeId");
    
            List blobs = BSF.getService().getUploads(req).get("image");
            BlobKey blobKey = blobs.get(0);
    
            ImagesService imagesService = ImagesServiceFactory.getImagesService();
            ServingUrlOptions servingOptions = ServingUrlOptions.Builder.withBlobKey(blobKey);
            String servingUrl = imagesService.getServingUrl(servingOptions);
    
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.setContentType("application/json");
    
    
            JSONObject json = new JSONObject();
            try {
                json.put("imageUrl", servingUrl);
                json.put("codeId", "picture_of_"+userId);
                json.put("blobKey",  blobKey.getKeyString());
            } catch (JSONException e){
    
                e.printStackTrace();            
            }
    
            PrintWriter out = resp.getWriter();
            out.print(json.toString());
            out.flush();
            out.close();
        }
    }
    

    and add to web.xml, where com.apppack is the package of Update Class

    
    update
    com.apppack.Update
    
    
    update
    /*
    
    

提交回复
热议问题