将Photo Android上传到Blobstore GAE

时间:2014-05-09 11:55:40

标签: java android google-app-engine rest blobstore

首先,我是法国人,请原谅我的英语..我已经走过stackoverflow找到我的答案,但没有成功..我是Android和Java语言的初学者。

我想从我的Android应用上传照片到Blobstore。我读了这些文章并取得了成功:

但我不知道如何在我的Android应用程序中编写代码以将照片上传到blobstore ...

这是我的代码:

Serve.java

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

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException {
            BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
            blobstoreService.serve(blobKey, res);
        }
}

Upload.java

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

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {

        Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
        BlobKey blobKey = blobs.get("myFile");

        if (blobKey == null) {
            res.sendRedirect("/");
        } else {
            res.sendRedirect("/serve?blob-key=" + blobKey.getKeyString());
        }
    }
}

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new LongOperation().execute("");
    }

     private class LongOperation extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {

                // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://1-dot-ptm-blobstore.appspot.com/upload");

                try {

                    // Execute HTTP Post Request
                    HttpResponse response = httpclient.execute(httppost);

                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                }   


                return null;

            }
        }
}

当我启动应用时,我的应用引擎日志出错:

Must be called from a blob upload callback request.

但我真的不是现在必须做的事情吗?你能帮助我吗 ? =) 提前谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个简单的解决方案:Upload to Appengine Blobstore in Android

另一个是使用云端点,即创建REST API端点并使用它来从您的应用发布图像数据: https://developers.google.com/appengine/docs/java/endpoints/

此外,更高级的示例是Mobile Backend Starter,它为您提供了一些更酷的功能: https://developers.google.com/cloud/samples/mbs/

这是一篇与Mobile Backend Starter合作的文章:

使用Google Cloud Endpoints将移动开发人员连接到云端 http://googlecloudplatform.blogspot.co.uk/2013/11/connecting-mobile-developers-to-the-cloud-with-google-cloud-endpoints.html