我想在android中使用http post发布多个字节数组。我使用BYTEARRAYENTITY成功发布单字节数组但是在单个请求中发布多个字节数组应该在代码中更改什么?
我编写如下代码,可以正常发布一个字节数组。但是等待解决方案发布多字节数组。
public String postData2s(String URL, String data) {
String filesizes = "";
BufferedReader bufferedReader = null;
String strresponse = "";
byte[] byteArray = null;
byteArray = data.getBytes();
filesizes = byteArray.length + "";
URL = URL + "&" + "fileSizes=" + filesizes;
final DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost request = null;
try {
request = new HttpPost(URL);
} catch (Exception e) {
}
HttpResponse response = null;
ByteArrayEntity arrayEntity = new ByteArrayEntity(byteArray);
try {
arrayEntity.setContentType("text/xml");
request.setEntity(arrayEntity);
response = httpClient.execute(request);
bufferedReader = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
String LineSeparator = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + LineSeparator);
}
bufferedReader.close();
Log.e("response ", stringBuffer.toString());
strresponse = stringBuffer.toString();
} catch (ClientProtocolException e) {
strresponse = "";
e.printStackTrace();
} catch (IOException e) {
strresponse = "";
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return strresponse;
}
我的C#同事使用以下代码为Windows发布多字节数组
webRequest.ContentLength = size;
Stream requestStream = webRequest.GetRequestStream();
// Escribo cada uno de los archivos en el stream
for (int i = 0; i < arrBytes.Length; i++)
requestStream.Write(arrBytes[i], 0, arrBytes[i].Length);
requestStream.Close();
如果有人对此有所了解,请尽快回答我
由于
答案 0 :(得分:0)
我使用以下方法发布数据。还包括byteArray作为字符串格式中的一个字段。您可以使用具有多个bytearray的多个字段,它将完成这项工作。
代码:
public static String UpdateUser(String userIdValue, String userNameValue, String nameValue,
String emailIdValue, String contryIdValue,String countryNameValue, String stateValue, String cityValue ,String imageBytes){
String res = null;
HttpPut request = new HttpPut(USER_URL);
//request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
//request.setHeader("user-agent", "Yoda");
try {
//-----------------
/*InputStream is = new BufferedInputStream (getResources().openRawResource(R.drawable.images));
JSONArray array=readBytes(is);
is.close(); */
//----------------
// Build JSON string
JSONStringer jObject = new JSONStringer()
.object()
//.key("Get")
// .object()
.key("Userid").value(userIdValue)
.key("UserName").value(userNameValue)
.key("Name").value(nameValue)
.key("EmailId").value(emailIdValue)
//.key("Password").value("")
.key("CountryId").value(contryIdValue)
.key("CountryName").value(countryNameValue)
.key("State").value(stateValue)
.key("City").value(cityValue)
.key("ImageByteArray").value(imageBytes)
/*.key("UserVerification").value("")
.key("City").value("")
.key("City").value("")*/
// .endObject()
.endObject();
StringEntity entity = new StringEntity(jObject.toString());
//System.out.println("Our Res: "+jObject.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
//res = response.toString();
int resCode = response.getStatusLine().getStatusCode();
System.out.println("Our Res: "+jObject.toString());
System.out.println("Result code: "+resCode);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line="";
StringBuffer returnFromServer = new StringBuffer();
while ((line=in.readLine())!=null){
returnFromServer.append(line);
}
//Toast what we got from server
//Toast.makeText(getApplicationContext(),returnFromServer.toString(), Toast.LENGTH_LONG).show();
System.out.println("Responce: "+returnFromServer.toString());
res = returnFromServer.toString();
if (entity != null){
entity.consumeContent();
}
// Toast.makeText(getApplicationContext(),response.getStatusLine().getStatusCode()+"", Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
System.out.println("Responcre is:"+res);
return res;
}
你只需要添加额外的参数作为你拥有的byteArray的数量。
希望它会对你有所帮助。
享受编码。