将多个值httpPost从android发送到WCF服务
这个问题可能看起来很傻,但我不想犯错误。 正如标题中所解释的,我想使用post方法向WS发送多个字段。简单来说,就是一个标题(一个字符串)和一个图像。 到目前为止,我已经成功地发送了图像(在经历了很多麻烦之后) 以下是android代码:将多个值httpPost从android发送到WCF服务,android,wcf,web-services,Android,Wcf,Web Services,这个问题可能看起来很傻,但我不想犯错误。 正如标题中所解释的,我想使用post方法向WS发送多个字段。简单来说,就是一个标题(一个字符串)和一个图像。 到目前为止,我已经成功地发送了图像(在经历了很多麻烦之后) 以下是android代码: String urlServer = GlobalSession.IP + "insert_reportByte"; URL url = new URL(urlServer); HttpURLConnection
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);
}
因此,我想从delcaration切换到public void insert\u reportByte(字符串名,流输入)
。如何做到这一点?还是我必须发送流中的所有内容,然后逐个恢复传输的参数
谢谢你的提示 您需要像这样在请求中添加名称-值对
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair(name, "myName"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
List name-valuepairs=new-ArrayList(1);
添加(新的BasicNameValuePair(名称,“myName”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
我希望这会有帮助!看答案 看起来您的问题是处理请求
对于android客户端,您可以看到,有一个
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);
从服务器端来看,您的方法只需要一个流参数。但您需要以不同的方式解析此流。是一个正在使用的示例。因此,您的方法应该如下所示:
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);
}
}
希望这能有所帮助