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){
  }
}

appengine の場合:

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's snippet

戦術的な核攻撃のブログ

助けていただければ幸いです。適切な質問をしていないようです atm

4

1 に答える 1

0

これは私が持っているもので、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()

私を逃してしまうことの1つは、「entity.addPart("string", stringBody);」に何が起こったのかです。blobstore オブジェクトの get_uploads の一部ではないようです

于 2012-07-11T20:54:24.553 に答える