我正在尝试使用 MultipartEntity 将文档文件上传到服务器。该文档应该上传到特定的用户帐户(即用户电子邮件)。所以我所做的是我有一个名称值对列表数组,其中包含用户电子邮件的键值对。鉴于 httppost.setEntity() 方法只能采用一个参数,我遇到了将 List 数组和 MultipartEntity 耦合到一个对象中的问题,这样我就可以说 httpPost.setEntity(objecy) (即对象是值namevaluepair 和 multipartity 的列表)。我怎样才能做到这一点。这是我到目前为止的代码。
@Override
protected String doInBackground(String... args) {
// uploadFile(filePath, fileName);
// get the data from the text fields
String email = sharedPref.getString("session", "no email");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
InputStream inputStream;
try {
inputStream = new FileInputStream(new File(filePath));
byte[] data;
try {
data = IOUtils.toByteArray(inputStream);
InputStreamBody inputStreamBody = new InputStreamBody(
new ByteArrayInputStream(data), fileName);
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("document", inputStreamBody);
//Here is where i need to figure out how to couple both the List and
//and multipart before making the httprequest
json = jsonParser.makeHttpRequest(url_upload_background,
"POST", multipartEntity);
Log.d("File to upload",
"Multipart is: " + multipartEntity.toString());
// check log cat for response
Log.d("Create Response", json.toString());
try {
error = json.getBoolean(TAG_ERROR);
message = json.getString(TAG_MESSAGE);
Log.d("Error is", "" + error);
Log.d("Message is", message);
if (error == false) {
// Log.d("account type before is ", accountType);
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
return null;
}
这是我需要 setEntity() 的 json 解析器的代码。
public JSONObject makeHttpRequest(String url, String method,
MultipartEntity multipartEntity) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(multipartEntity); //HERE
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
最佳答案
我终于明白了。为了将名称值对的数组列表与多方实体相结合,我将数组列表作为 StringBody 对象传递给多方实体,并将电子邮件的键分配给 StringBody。
@Override
protected String doInBackground(String... args) {
// uploadFile(filePath, fileName);
// get the data from the text fields
String email = sharedPref.getString("session", "no email");
List<NameValuePair> params = new ArrayList<NameValuePair>();
Log.d("THE EMAIL IS: ", email);
params.add(new BasicNameValuePair("email", email));
InputStream inputStream;
try {
inputStream = new FileInputStream(new File(filePath));
byte[] data;
try {
data = IOUtils.toByteArray(inputStream);
Log.d("File size", ""+ data.toString());
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), fileName);
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("document", inputStreamBody);
multipartEntity.addPart("email", new StringBody(email)); //HERE IS THE FIX
json = jsonParser.makeHttpRequest(url_upload_background,
"POST", multipartEntity);
修复是
multipartEntity.addPart("email", new StringBody(email));
关于android - 如何耦合 MultipartEntity 和 List 数组以在 android 中发送 httppost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27209283/