Why does the callback fail after uploading an image to the Blobstore?

I am trying to upload photos to blobstore. The photo uploads successfully to blobstore, but I can't get blobkey and serveUrl back. I tried debugging and saw that the callback is not being executed in BlobUpload.java. It gives the error "Connection to http: // localhost: 8888 refused" I am confused because BlobUrlGet.java calls without issue and loads the photo. I've tried all types of server and servlet urls, none of them worked.

EDIT: I realized that if I manually change "localhost: 8888" to "10.0.2.2: 8888" in debug mode (str variable) the problem "refused connection" disappears, this time below error.

http: // localhost: 8888 / _ah / upload / ahFteWZpcnN0YXBwZGVtbzEyM3IiCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGICAgICAgN4KDAError 405 HTTP POST method is not supported by this URL

Error 405 HTTP POST method is not supported by this URL

This is the return from the post method. This is the same error when I deploy to the application and work from there.

My code looks like this.

BlobUrlGet.java

public class BlobUrlGet extends HttpServlet{   
    BlobstoreService blServ = BlobstoreServiceFactory.getBlobstoreService();    
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {  
        String blobUploadUrl = blServ.createUploadUrl("/blob");   
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.setContentType("text/plain");    
        PrintWriter out = resp.getWriter();
        out.print(blobUploadUrl);
    }       
}

      

BlobUpload.java

public class BlobUpload extends HttpServlet {
    BlobstoreService blobstoreService = BlobstoreServiceFactory
            .getBlobstoreService();

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        try {
            List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
            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();

            json.put("servingUrl", servingUrl);
            json.put("blobKey", blobKey.getKeyString());

            PrintWriter out = resp.getWriter();
            out.print(json.toString());
            out.flush();
            out.close();
        } catch (JSONException e) {

            e.printStackTrace();
        }

    }

      

Android client

private class GetBlobUrlTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0){  

        HttpClient httpClient = new DefaultHttpClient();    
        //This will invoke "ImgUpload servlet           
        HttpGet httpGet = new HttpGet("http://10.0.2.2:8888/bloburl"); 
        HttpResponse response;
        try {
            response = httpClient.execute(httpGet);         
            HttpEntity urlEntity = response.getEntity();
            InputStream in = urlEntity.getContent();
            String str = ""; 
            StringWriter writer = new StringWriter();
            String encoding = "UTF-8";
            IOUtils.copy(in, writer, encoding);
            str = writer.toString();
//              str = URLDecoder.decode(str, "UTF-8");
                HttpPost httppost = new HttpPost(str);
                File f = new File(picturePath);
                FileBody fileBody = new FileBody(f);
                MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
                reqEntity.addPart("file", fileBody);
                reqEntity.addBinaryBody("Photo", f, ContentType.create("image/jpeg"), "foto2.jpg");
                httppost.setEntity(reqEntity.build());
                response = httpClient.execute(httppost); //Here "uploaded" servlet is automatically       invoked
                urlEntity = response.getEntity(); //Response will be returned by "uploaded" servlet in JSON format
                in = urlEntity.getContent();
                str = "";
                IOUtils.copy(in, writer, encoding);
                str = writer.toString();
                JSONObject resultJson = new JSONObject(str);
                blobKey = resultJson.getString("blobKey");
                servingUrl = resultJson.getString("servingUrl");    



        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

      

web.xml

  <servlet>
    <servlet-name>BlobstoreURL</servlet-name>
    <servlet-class>com.google.samplesolutions.mobileassistant.BlobUrlGet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BlobstoreURL</servlet-name>
    <url-pattern>/bloburl</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>BlobstoreUpload</servlet-name>
    <servlet-class>com.google.samplesolutions.mobileassistant.BlobUpload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BlobstoreUpload</servlet-name>
    <url-pattern>/blob</url-pattern>
  </servlet-mapping>

      

Thanks in advance.

+3


source to share


1 answer


doPost must be overridden. It works fine now.



public class BlobUpload extends HttpServlet {
    BlobstoreService blobstoreService = BlobstoreServiceFactory
            .getBlobstoreService();
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        try {
            List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
            BlobKey blobKey = blobs.get(0);

      

+2


source







All Articles