5

我可以使用 StringEntity 发布 json 对象

httppost.setEntity(newStringEntity(obj.toString())); HttpResponse 响应 = httpclient.execute(httppost);

但我必须与 json 数据一起发布一个文件,我找到了很多使用 MultipartEntity 的答案,但它已被弃用,请向我推荐任何使用 MultipartEntityBuilder 的教程或示例代码,谢谢
这是我的代码

Thread t = new Thread(new Runnable() {

@Override
public void run() {

JSONObject jsonobject = null;

String requestURL = "URL";

try {

jsonobject = new JSONObject("{\"first\": \"Test\",\"last\": \"User\",\"name\": \"Test 
User\",\"email\": \"tr0121345899@gmail.com\",\"birthdate\": \"1984-01-01\",\"Account\": {\"username\": \"t1r0123425899\",\"password\": \"testuser1234567899\"},\"Address\": {\"postal_code\": \"11230\",\"state_or_province\": \"NY\",\"country\": \"US\"}}");
                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }



HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(requestURL);

httppost.addHeader("Content-Type", "application/json");

httppost.addHeader("X-ConsumerApiClient","0f8f0a024d6344e429f5ee96aa66fbfb5c3973b5");

httppost.addHeader("X-ConsumerApiSignature",

                    "qmt8aEAGRQUvldkDnHw8zgn1kRYuXRDwvmo3TpWlCtE=");

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
                multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

multipartEntity.addPart("file", new FileBody(new
                 File(fileName)));
multipartEntity.addPart("Person", jsonobject.toString());

httppost.setEntity(multipartEntity.build());


try {

     HttpResponse response = httpclient.execute(httppost);

     Log.d("status", "" + response.getStatusLine());

     Log.d("response",EntityUtils.toString(response.getEntity()));

    } catch (ClientProtocolException e) {

    } catch (IOException e) {


}
}
});

t.start();
4

3 回答 3

4

我找到了解决方案,我们可以像下面这样拆分 json 对象并作为键值发送

  multipartEntity.addPart("data[Asset][file]", new FileBody(file,
                            ContentType.APPLICATION_OCTET_STREAM, "filename.png"));
                    multipartEntity.addTextBody("data[Person][first]", "kk");
                    multipartEntity.addTextBody("data[Person][email]",
                            "rajesh123458@gmail.com");
                    multipartEntity.addTextBody("data[Person][birthdate]",
                            "1984-01-01");
                    multipartEntity.addTextBody("data[Person][Account][username]",
                            "savita123458");
                    multipartEntity.addTextBody("data[Person][Account][password]",
                            "testuser12345678");
        httppost.setEntity(multipartEntity.build());
                    try {
                        HttpResponse response = httpclient.execute(httppost);
                        Log.d("status", response.getStatusLine().toString());

                        Log.d("data", EntityUtils.toString(response.getEntity()));

                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace(); // TODO Auto-generated catch block
                    }
于 2014-02-22T08:49:27.157 回答
0

我使用http://loopj.com/android-async-http/做了类似的事情,可以在你的线程中同步使用

于 2014-02-11T10:31:30.350 回答
0

尝试这样做。

multipartEntity.addBinaryBody("file", file, ContentType.create("application/octet-stream"), file.getName());
multipartEntity.addTextBody("json", jsonobject.toString(),  ContentType.DEFAULT_BINARY);

httppost.setEntity(multipartEntity.build());
于 2016-07-20T06:08:29.227 回答