从android我发送图像到appengine servlet,我尝试从blobstore获得响应,但没有成功

时间:2015-11-30 19:35:22

标签: android image servlets post blobstore

我使用以下代码从blobstore获取blob引用

//Connect with the ImgUpload servlet to get UploadStrurl   
public class getUrl implements Runnable
{ 
    HttpGet httpGet=new HttpGet();
    org.apache.http.HttpResponse response=null;
                @Override
                public void run() {
                    try {
                         HttpClient httpClient = new DefaultHttpClient();
                         httpGet = new HttpGet(url_get);
                         response = httpClient.execute(httpGet);
                         HttpEntity entity = response.getEntity();     

                         responseBlobUrlString=EntityUtils.toString(entity);
                    } catch (ClientProtocolException e) {
                        Log.e(TAG, "ClientProtocolException", e);
                    } catch (ParseException e) {
                        Log.e(TAG, "ParseException", e);
                    } catch (IOException e) {
                        Log.e(TAG, "IOException", e);
                    }   

          }

以上代码经过测试且运行正常。 要响应的servlet代码:

     public class TestServletServlet extends HttpServlet {
    com.google.appengine.api.blobstore.BlobstoreService blobstoreService = BlobstoreServiceFactory
            .getBlobstoreService();
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
String blobUploadUrl = blobstoreService.createUploadUrl("/upload"); 

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

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



    }

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

    }
}

以上代码经过测试且运行正常。 我使用以下代码发布我的图片:

public static String executeHttpPost(String url, String image_path) throws Exception {
    InputStream in = null;
    HttpEntity entity1=null;
    MultipartEntity mpentity = new MultipartEntity();
    HttpClient client = getHttpClient();
    org.apache.http.HttpResponse response = null;
    HttpPost request;
    String responseBody = null;
    String result=null;
    HttpParams params = (HttpParams) new BasicHttpParams();


        request = new HttpPost(url);


        File file=new File(image_path);
        FileBody fb=new FileBody(file);
        mpentity.addPart("file", fb);
        request.setEntity(mpentity);

        try{
        response = client.execute(request);
        HttpEntity urlEntity = response.getEntity();


        if (response.getEntity().getContentLength()!=0)
            result=response.getEntity().toString();
        else
            result="0";

            return result;

            } 
        catch (Exception e) {
                e.toString();
            } 
        return result;

    } 

以上代码经过测试且运行正常。 来自servlet端的响应:

public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
             List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
            BlobKey blobKey = blobs.get(0);


            ImagesService imagesService = (ImagesService) ImagesServiceFactory.getImagesService();
            ServingUrlOptions servingOptions = ServingUrlOptions.Builder.withBlobKey(blobKey);

            String servingUrl = ((com.google.appengine.api.images.ImagesService) imagesService).getServingUrl(servingOptions);

            resp.setStatus(HttpServletResponse.SC_OK);
             resp.setContentType("application/json");

            JSONObject json = new JSONObject();
            try {
                json.put("servingUrl", servingUrl);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                json.put("blobKey", blobKey.getKeyString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            PrintWriter out = resp.getWriter();
            out = resp.getWriter();
            out.print(json.toString());
            out.flush();
            out.close();

        }

这段代码没有按预期返回blobkey和服务url。 我通过异步任务激活HttpPost,如下所示:

 class TheTask extends AsyncTask<String,String,String>
    {
    String response = null;



protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    //error message
    debuger.setText("the returned response is:"+result);
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
    JSONObject resultJson=null;
            try {

                 blobKey= CustomHttpClient.executeHttpPost(params[0], photo_striped);

                }
            catch(Exception e)
            {
                blobKey="exception in geting resp";
                e.printStackTrace();
            }

            try {

                resultJson = new JSONObject(blobKey);
                blobKey = resultJson.getString("blobKey");
                servingUrl = resultJson.getString("servingUrl");
            } catch (JSONException e) {
                e.printStackTrace();
            }


                return blobKey;
         }

}

以上代码经过测试且运行正常。 我使用下面的codelet在我的活动中执行我的异步任务:

 back.setOnClickListener(new OnClickListener() {                            
                                @Override
                                public void onClick(View v) {
                                     ViewFlipper vf = (ViewFlipper) findViewById( R.id.viewFlipper );
                                     vf.showNext(); 

                                    new TheTask().execute(responseBlobUrlString);
                                }
                            });

我的web.xml是:

    <?xml version="1.0" encoding="utf-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
 <servlet>
  <servlet-name>SystemServiceServlet</servlet-name>
  <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
  <init-param>
   <param-name>services</param-name>
   <param-value>com.alphaapps.quoteserver.QuoteEndpoint</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>SystemServiceServlet</servlet-name>
  <url-pattern>/_ah/spi/*</url-pattern>
 </servlet-mapping>



  <servlet>
     <servlet-name>uploaded</servlet-name>
     <servlet-class>com.alphaapps.quoteserver.uploaded</servlet-class>
     </servlet>

     <servlet-mapping>
            <servlet-name>uploaded</servlet-name>
            <url-pattern>/upload</url-pattern>

    </servlet-mapping>



   <servlet>
        <servlet-name>Test</servlet-name>
        <servlet-class>com.alphaapps.quoteserver.TestServletServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Test</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>

     <servlet>
        <servlet-name>Serve</servlet-name>
        <servlet-class>com.alphaapps.quoteserver.Serve</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Serve</servlet-name>
        <url-pattern>/serve</url-pattern>
    </servlet-mapping>



</web-app>

请帮我解决这个错误。上传图片但没有得到blobstore的回复真是一件令人讨厌的事...... 当我检查响应时它没有打印任何内容,这意味着blobstore没有返回任何blobkey或任何其他值......

0 个答案:

没有答案