【发布时间】:2014-03-30 10:45:38
【问题描述】:
我已经阅读了很多帖子来完成任务,比如在 android 中使用 multipartentity 和 josn 上传图像以及参数,但我的问题是当我尝试上传图像和参数而不将字符串转换为 JSONobject 然后图像有上传时没有错误,但是当我尝试将响应字符串添加到 jsonobject 时,logcat 中会出现错误,例如):
error: Value`<form of type java.lang.String cannot be converted to JSONObject.
有人请帮忙解决这个问题吗?我想用MultipartEntity 将图像和JsonObject 发送到PHP 服务器。我正在开发一个允许用户使用 HttpPost 方法上传图像的应用程序。我使用MultipartEntity,因此我将库 apache-mime4j-0.6.1.jar、httpclient-4.3.1.jar、httpcore-4.3.1.jar 和 httpmime-4.2.1.jar 添加到我的应用程序中。
这是我的代码:
public JSONObject doFileUpload(String _fname, String _lname, String _email,
String _password, String _country, String _countrycode,
String _phone) {
File file1 = new File(selectedPath);
String urlString = "http://capstonehostingservices.com/fetch_new/app/index.php/fetch/register";
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urlString);
FileBody bin1 = new FileBody(file1);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("photo", bin1);
reqEntity.addPart("first_name", new StringBody(_fname));
reqEntity.addPart("last_name", new StringBody(_lname));
reqEntity.addPart("email", new StringBody(_email));
reqEntity.addPart("password", new StringBody(_password));
reqEntity.addPart("country", new StringBody(_country));
reqEntity.addPart("country_code", new StringBody(_countrycode));
reqEntity.addPart("phone", new StringBody(_phone));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
String response_str = EntityUtils.toString(resEntity);
json = new JSONObject(response_str);
} catch (Exception ex) {
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
return json;
}
在上面的代码中,我试图将响应字符串转换为 jsonobject,我该如何实现呢?
我使用 selectedpath 参数从图库中获取图像路径,我想将图像和JsonObject 发送到带有MultipartEntity 的 PHP 服务器。我正在开发一个允许用户使用HttpPost 方法上传图像的应用程序。我使用MultipartEntity,因此我将库 apache-mime4j-0.6.1.jar、httpclient-4.3.1.jar、httpcore-4.3.1.jar 和 httpmime-4.2.1.jar 添加到我的应用程序中。
【问题讨论】: