开发者

Post values and upload Image to php server in android

开发者 https://www.devze.com 2023-02-03 04:38 出处:网络
I am trying to upload image from android phone to php server with additional values,the method is post

I am trying to upload image from android phone to php server with additional values,the method is post my php file look like this

if($_POST['val1']){
  if($_POST['val2']){
     if($_FILE['image']){
      ...... 
    }
  }
}else{
echo "Value not found";
}

I am doing is

URL url=new URL("http://www/......../myfile.php")开发者_运维问答;
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");//Enable http POST
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "multipart/form-data;boundary="+"****");
connection.setRequestProperty("uploaded_file", imagefilePath);
DataOutputStream ostream = new DataOutputStream( con.getOutputStream());
String res=("Content-Disposition: form-data; name=\"val1\""+val1+"****"+
"Content-Disposition: form-data; name=\"val2\""+val2+"****"
"Content-Disposition: form-data; name=\"image\";filename=\"" + imagefilePath +"\""+"****");
outputStream.writeBytes(res);

my actual problem is values are not posting so first if condition get false and else section is executed that is it give value not found please help me


I have found solution of my problem. I am making mistake at line string res=(" .... "); I have not adding "\r\n" after every content.I have add it and my code working properly

link is very useful for me : http://wiki.forum.nokia.com/index.php/HTTP_Post_multipart_file_upload_in_Java_ME

Thank you sir for helping me


here is a nice article, http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/. It works.

0

精彩评论

暂无评论...
验证码 换一张
取 消