使用HTTP方法从blobstore检索blob密钥

时间:2015-07-01 12:50:10

标签: java android html google-app-engine google-cloud-storage

我一直在提及这篇文章Store image to Blobstore from android client and retrieve blobkey and upload url to store in Datastore. - GAE。下面的最后两个代码块可能是最相关的,因为它们是在app引擎和我的Android应用程序之间发送和接收blob密钥的代码。

blob上传得很好。 当我让我的servlet返回blob密钥时,我继续在我的日志中得到这个:

405 HTTP method POST is not supported by this URL

以下是应用引擎上的BlobUrlGet.java,它提供了上传blob的URL:

public class BlobUrlGet extends HttpServlet {


BlobstoreService blServ =    BlobstoreServiceFactory.getBlobstoreService();

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {


    UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName("abc123-974.appspot.com");
    String blobUploadUrl = blServ.createUploadUrl("/Uploaded", uploadOptions);




    resp.setStatus(HttpServletResponse.SC_OK);
    resp.setContentType("text/plain");

    PrintWriter out = resp.getWriter();
    out.print(blobUploadUrl);
}

}

然后我在我的Android应用程序中使用此代码上传文件,然后侦听blob键:

private class GetBlobUrlTask extends AsyncTask<Void, Void, Void> {
    HttpResponse response = null;

    protected Void doInBackground(Void... arg0){  
HttpClient httpClient = new DefaultHttpClient();  

HttpGet httpGet = new HttpGet("http://abc123-974.appspot.com/bloburlget"); 


        response = httpClient.execute(httpGet);


HttpEntity urlEntity = response.getEntity();
InputStream in = null;
        in = urlEntity.getContent();


}
String str = ""; 
StringWriter writer = new StringWriter();
String encoding = "UTF-8";


    IOUtils.copy(in, writer, encoding);

str = writer.toString();



@SuppressWarnings("deprecation")
HttpPost httppost = new HttpPost(str);

File f2 = new File(Environment.getExternalStorageDirectory()
        + "/ContactDetail.json");
FileBody fileBody = new FileBody(f2);


MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", fileBody);
httppost.setEntity(reqEntity);

    response = httpClient.execute(httppost);




String str2 = "";

str2 = EntityUtils.toString(response.getEntity());

//Getting that Post is not supported method when I print the following string:
blobKeyString = str2;

最后我的Uploaded.java应该返回blob键,但不是:

public class Uploaded extends HttpServlet {


private static final long serialVersionUID = 1L;
BlobstoreService blobstoreService = BlobstoreServiceFactory
        .getBlobstoreService();

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

            List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");

            BlobKey blobKey = blobs.get(0);

            resp.setStatus(HttpServletResponse.SC_OK);
            resp.setContentType("text/plain");

            JSONObject json = new JSONObject();

           String blobKeyString = blobKey.getKeyString();

            PrintWriter out = resp.getWriter();
            out.print(blobKeyString);
            out.flush();
            out.close();

为什么我的字符串blobKeyString在将其返回到Android应用程序时,继续在主体中具有html文档的值:

405 HTTP method POST is not supported by this URL

1 个答案:

答案 0 :(得分:2)

查看错误,这意味着您需要一种方法来处理对您的应用的POST请求。

我会更改接收请求的URL上的servlet,而不是“doGet”。