0

The question could seems silly but I don't want to do a mistake. As explained in the title, I want to send multiple fields to a WS using post method. To be simple, a title (a string), and an image. So far, I have succedded into sending the image (after A LOT of troubles).

Here is the android code:

String urlServer = GlobalSession.IP + "insert_reportByte";
            URL url = new URL(urlServer);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type","multipart/form-data");

            DataOutputStream outputStream = new DataOutputStream(
                    connection.getOutputStream());

            outputStream.write(outputByteArray, 0, outputByteArray.length);
            Log.d("Report", " Amount of data sent for the picture: " + outputByteArray.length);

            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();
            outputStream.flush();
            outputStream.close();

that is sucessfuly sending the picture (outputByteArray) the the following WS:

public void insert_reportByte(Stream input) 
    {
        MyEntities entities = new MyEntities();

        byte[] image = new byte[30*1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(image, 0, image.Length)) > 0)
            {
                ms.Write(image, 0, read);
            }
        }

        String base64stringimage = System.Convert.ToBase64String(image,0,image.Length);
        entities.insert_report("name hard coded", base64stringimage);
    }

So, I want to switch from that delcaration to public void insert_reportByte(String name, Stream input). How to do so? Or do I have to send everything in the stream an then recovering one by one the parameters transmitted?

Thanks for tips!

Derbie
  • 413
  • 1
  • 12
  • 28

2 Answers2

1

Look like your problem is handle Multipart data request.
For the android client side, you could look at this answer, there is a MultipartEntity which is multipart/form coded HTTP entity consisting of multiple body parts. Your request implement look like this:

   HttpPost httppost = new HttpPost(urlServer);
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
    multipartEntity.addPart("name", new StringBody("your input name here"));
    multipartEntity.addPart("Image", new FileBody(new File(imagePath)));
    httppost.setEntity(multipartEntity);
    mHttpClient.execute(httppost);

And from the server side, you method just needs only one stream parameter. But you need to parse this stream in difference way.Here is an example which is use form parser library. Therefore, your method should look like:

public void insert_reportByte(Stream stream)
{
   HttpMultipartParser parser = new HttpMultipartParser(data, "Image");

if (parser.Success)
{
    // get the name from android client
    string name = HttpUtility.UrlDecode(parser.Parameters["name"]);

    // Save the file somewhere
    File.WriteAllBytes(your_server_file_path, parser.FileContents);
}
} 

Hope this can help.

Community
  • 1
  • 1
ductran
  • 10,043
  • 19
  • 82
  • 165
0

You need to add NameValue pairs in your request like this!

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);  
nameValuePairs.add(new BasicNameValuePair(name, "myName"));    
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

I hope it will help! See this answer!

Community
  • 1
  • 1
Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81