使用multipartentity和JSON android中上传图片到服务器 [英] Upload image to the server using multipartentity and json in android
问题描述
我看了很多帖子来实现任务喜欢使用Android中multipartentity和josn参数一起上传图片,但我的问题是,当我试图上传图片和参数,而不将字符串转换为 JSONObject的
然后图像上传没有错误,但在像logcat中当我试图添加响应字符串到JSONObject的再发生错误):
I have read lots of post to achieve the task like to upload image along with parameters using multipartentity and josn in android, but my problem is when i was trying to upload image and parameter without converting string to JSONobject
then image has uploaded without an error but when i was trying to add response string to jsonobject then error occur in the logcat like ):
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到我的应用程序。
Any one please help to resolve this issue? I want to send an image and JsonObjec
t to an PHP Server with MultipartEntity
. I am working on an app that allows the user upload an image by using HttpPost
method. I use MultipartEntity
and therefore I added the libraries apache-mime4j-0.6.1.jar, httpclient-4.3.1.jar, httpcore-4.3.1.jar and httpmime-4.2.1.jar into my app.
下面是我的code:
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;
}
在上面code我试图响应字符串转换的JSONObject,如何达到这个目标?
我用selectedpath参数作为从GALLARY获得图片路径,我想发送图像和的JSONObject
来与PHP服务器 MultipartEntity
。我工作的一个应用程序,允许用户通过使用 HttpPost
方法上传图片。我使用 MultipartEntity
,所以我增加了库Apache的mime4j-0.6.1.jar,HttpClient的-4.3.1.jar,的HttpCore-4.3.1.jar和httpmime- 4.2.1.jar到我的应用程序。
In above code i was trying to convert response string to jsonobject, how to i achieve this?
I used selectedpath parameter as to get image path from gallary, I want to send an image and a JsonObject
to an PHP Server with MultipartEntity
. I am working on an app that allows the user upload an image by using HttpPost
method. I use MultipartEntity
and therefore I added the libraries apache-mime4j-0.6.1.jar, httpclient-4.3.1.jar, httpcore-4.3.1.jar and httpmime-4.2.1.jar into my app.
推荐答案
这可能不是最好的答案,但它是开始一个很好的例子。
This may not be the best answer but it is a very good example to get started.
使用下面的函数发送文件到服务器:
Use the following function to send file to server:
public static void postFile(String fileName) throws Exception {//fileName is path+filename of picture
String url_upload_image = "http://url-to-api/upload_photo.php";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url_upload_image);
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("file", new FileBody(new File(fileName)));
post.addHeader("id", id);//id is anything as you may need
post.setEntity(multipartEntity.build());
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
entity.consumeContent();
client.getConnectionManager().shutdown();
}
PHP文件,该文件是在 HTTP://url-to-api/upload_photo.php
<?php
$name = $_POST['id'];
$file_path = "images/";
$file_path = $file_path . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $file_path)) {
echo "success";
echo $name;
} else{
echo "fail";
}
?>
并确保你的服务器目录或文件夹是可执行的,可读写。我有这样的重大问题。这就是称为777权限 ..相信我,这是因为其他的事情要考虑的重要。
And make sure that your directory or folder in server is Executable, Writable and Readable. I had this as the major problem. This is called 777 permission.. Believe me, this is as important as other things to consider.
这篇关于使用multipartentity和JSON android中上传图片到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!