我已经阅读了很多帖子来实现上传图像以及使用android中的multipartentity和josn参数的任务,但我的问题是当我试图上传图像和无需转换字符串JSONobject
参数,然后图像上传没有错误,但是当我尝试添加响应字符串到JSONObject的再发生类似的logcat的)错误:上传图像到服务器使用multipartentity和json在android
error: Value`<form of type java.lang.String cannot be converted to JSONObject.
任何一个请帮助解决这个问题?我想发送一个图像和JsonObjec
t到一个PHP服务器与MultipartEntity
。我正在开发一个应用程序,允许用户使用H ttpPost
方法上传图像。我使用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参数作为从gallary得到图像路径,我想发送一个图像和一个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添加到我的应用程序中。