1

我一直在努力将照片图像从 android 上传到 python appengine 这是我在 Android 中尝试过的:

void apachePost()  throws Exception {
    File image = new File("/sdcard/image.jpg");
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://clockinapple.appspot.com/upload");
    try {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("type", new StringBody("photo"));
    entity.addPart("data", new FileBody(image));
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
    Log.v(Constants.DATA, "received http response " + response);
    } catch (ClientProtocolException e){
  }
}

在应用引擎中:

class UserPhoto(db.Model):
    user = db.StringProperty()
    blob_key = blobstore.BlobReferenceProperty()

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload = self.get_uploads()[0]
        user_photo = UserPhoto(user="test", blob_key=upload.key())
        db.put(user_photo)
        return user_photo.key()

我记录的服务器错误是“Apache-HttpClient/UNAVAILABLE (java 1.4)”

我认为标题不正确 - 我尝试了很多变体

一些链接已经尝试过: Ika Lan 的片段

战术核打击博客

我真的很感激任何帮助,我似乎没有问正确的问题

4

1 回答 1

0

这就是我所拥有的(更改为 HttpGet),Android 代码:

void apachePost(String url, String filename)  throws Exception {
    HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);

HttpResponse urlResponse = httpClient.execute(httpGet);

String result = EntityUtils.toString(urlResponse.getEntity());

Uri fileUri = Uri.parse(filename); // Gets the Uri of the file in the sdcard
File file = new File(new URI(fileUri.toString())); // Extracts the file from the Uri

FileBody fileBody = new FileBody(file, "multipart/form-data");
StringBody stringBody = new StringBody("Arghhh");

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", fileBody);
entity.addPart("string", stringBody);

HttpPost httpPost = new HttpPost(result);

httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost);
response.getStatusLine();
    Log.v(Constants.DATA, "received http response " + response);
    Log.v(Constants.DATA, "received http entity " + entity);
}

Appengine 代码:

class GetBlobstoreUrl(BaseHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload/')
    logging.debug(upload_url)
        self.response.out.write(upload_url)

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        text_files = self.get_uploads('string')
        blob_info = upload_files[0]
        user_info = "text_files"
        photo = clockin.UserPhoto(blob_key=blob_info.key(), user=user_info)
        photo.put()

让我难以理解的一件事是“entity.addPart(“string”,stringBody);“ 它似乎不是 blobstore 对象中 get_uploads 的一部分

于 2012-07-11T20:54:24.553 回答