【问题标题】:Send multiple values httpPost from android to WCF service将多个值 httpPost 从 android 发送到 WCF 服务
【发布时间】:2014-01-27 23:32:47
【问题描述】:

这个问题可能看起来很傻,但我不想犯错。 如标题中所述,我想使用 post 方法将多个字段发送到 WS。简单地说,一个标题(一个字符串)和一个图像。 到目前为止,我已经成功发送了图像(经过很多麻烦)。

这里是安卓代码:

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();

即成功将图片(outputByteArray)发送到以下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);
    }

所以,我想从那个声明切换到public void insert_reportByte(String name, Stream input)。怎么做?还是我必须发送流中的所有内容,然后一一恢复传输的参数?

感谢您的提示!

【问题讨论】:

    标签: android wcf web-services


    【解决方案1】:

    看起来您的问题是处理 Multipart data 请求。
    对于 android 客户端,您可以查看 this answer,有一个 MultipartEntity,它是由多个主体部分组成的多部分/表单编码的 HTTP 实体。您的请求实现如下所示:

       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);
    

    从服务器端,你的方法只需要一个流参数。但是您需要以不同的方式解析此流。Here 是一个使用form parser library 的示例。因此,您的方法应如下所示:

    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);
    }
    } 
    

    希望这能有所帮助。

    【讨论】:

      【解决方案2】:

      您需要像这样在请求中添加 NameValue 对!

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

      我希望它会有所帮助!见this答案!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多