java - 使用嵌套在 JAVA android 中格式化 HTTP Post 请求参数

标签 java android http-post paperclip multipartentity

我正在尝试将以下数据发布到我的 Rails 应用程序。它期望格式如下:

Parameters: {
"utf8"=>"✓", 

"authenticity_token"=>"oSJ2ut0T1HGJ+KcBAPPP4lwn8Hc4Xwkn8emVujXy9xQ=", 

"wine"=>{"name"=>"nice wine", "vintage"=>"1923", "price"=>"412.2", "photo"=>#<ActionDispatch::Http::UploadedFile:0x007f9c86243d38 @tempfile=#<Tempfile:/var/folders/z9/r6hjby6x2cvfrlldtv7djz8c0000gn/T/RackMultipart20140825-4734-em8b0l>, @original_filename="timtams.jpeg", 
@content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"wine[photo]\"; filename=\"timtams.jpeg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Create Wine"}

但是,我现在从我的 android 应用程序中得到这个:

      Parameters: {
    "photo"=>#<ActionDispatch::Http::UploadedFile:0x007f8b69bd2968 @tempfile=#<Tempfile:/var/folders/z9/r6hjby6x2cvfrlldtv7djz8c0000gn/T/RackMultipart20140827-3309-f5b613>, @original_filename="20140608_172146-3-1.jpg", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"photo\"; filename=\"20140608_172146-3-1.jpg\"\r\nContent-Type: application/octet-stream\r\n">, 
"name"=>"test1", 
    "vintage"=>"1927", 
    "price"=>"19.27"}

有没有办法在参数前面定义“wine”?

以下是我目前的代码:

/**
 * Created by zhongqinng on 26/8/14.
 */
public class UploadAsync extends AsyncTask {

    private String TAG = "UploadAsync";
    @Override
    protected Object doInBackground(Object[] params) {
        postMyWine2();
        return null;
    }

    public void postMyWine2(){
        Log.i(TAG, "postMyWine2 start");
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(WineStoryHTTPClient.BASE_URL + "/wines");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        File photofile = new File("/storage/sdcard0/Download/20140608_172146-3-1.jpg");

        builder.addPart("photo", new FileBody(photofile));
        builder.addTextBody("name", "test1");
        builder.addTextBody("vintage", "1927");
        builder.addTextBody("price", "19.27");



        post.setEntity(builder.build());

        Log.i(TAG,"postMyWine2 post.getParams() = "+post.getParams());
        try {
            Log.i(TAG,"postMyWine2 posting");
            HttpResponse response = client.execute(post);
        } catch (Exception e) {
            Log.i(TAG,"postMyWine2 post Exception");
            e.printStackTrace();
        }
        //  HttpEntity entity = response.getEntity();
    }
}

最佳答案

设法通过在服务器端实现解决方法来解决问题: 更改了服务器端期望的格式。 但是我仍然有兴趣了解如何从客户端格式化帖子参数。

  def create
    # @wine = Wine.new(wine_params)
    @wine = Wine.new( :name => params[:name], :vintage => params[:vintage], :price => params[:price] ,:photo => params[:photo])
    respond_to do |format|
      if @wine.save
        format.html { redirect_to @wine, notice: 'Wine was successfully created.' }
        format.json { render :show, status: :created, location: @wine }
      else
        format.html { render :new }
        format.json { render json: @wine.errors, status: :unprocessable_entity }
      end
    end
  end

关于java - 使用嵌套在 JAVA android 中格式化 HTTP Post 请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25511227/

相关文章:

android - HttpPost + setEntity : how to know content-length?

java - hbase:使用动态创建的限定符查询特定值

java - 可以在 switch 语句中使用 throw 代替 break 吗?

Java - 尽管类存在但出现 NoClassDefFoundError

android - 在 Android Studio 中使用自定义 android.jar

android - testProguardFiles 忽略了我的规则。无法针对发布构建类型运行 Espresso 测试

python - flask : Ignore if one file is not uploaded in form (containing multiple file upload options)

java - 将数组传递给 Activity (Android)

android - Espresso + mockito - 模拟 Activity 启动

c# - 'HttpRequest' 不包含 'Files' 的定义