C# 无法转换类型';字符串';至';字节[]和#x27;
我正在尝试从android发送和从.net页面获取,我可以获取字符串参数,但我很难获取字节数组 安卓C# 无法转换类型';字符串';至';字节[]和#x27;,c#,android,.net,C#,Android,.net,我正在尝试从android发送和从.net页面获取,我可以获取字符串参数,但我很难获取字节数组 安卓 HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(urls[0]); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urls[0]);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.addTextBody("X", "Y");
File photo= new File(Environment.getExternalStorageDirectory(), "photo.jpg");
if(photo != null)
{
entityBuilder.addBinaryBody("IMAGE", photo);
}
HttpEntity entity = entityBuilder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(httpEntity);
.NET
试试1
这就是我如何解决这个问题,如果其他人也需要的话 安德烈
File photo = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photo.getAbsolutePath(), options);
//selected_photo.setImageBitmap(bitmap);
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(urls[0]);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
entityBuilder.addPart("IMAGE", bab);
}
catch(Exception e){
//Log.v("Exception in Image", ""+e);
}
HttpEntity entity = entityBuilder.build();
post.setEntity(entity);
HttpResponse response = null;
try {
response = httpClient.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String sResponse;
StringBuilder s = new StringBuilder();
try {
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
.NET
错误消息会准确地告诉您问题所在。。你知道字符串和字节[]之间的区别吗,甚至字符串和这个符号之间的区别吗?一个是一个数组,另一个不是。。此外,您还可以随时查看源代码。。所以
使用调试器代码>例外情况在哪里?它在这里:(byte[])Request.Params[“IMAGE”]代码>?如果是这样,请检查以下内容:您将图像写入body(addBinaryBody
)并尝试从url(Request.Params
)读取(顺便说一句:这是开发web应用程序的一个非常好的工具)这实际上已经在这里得到了回答:乍一看,您发送的代码似乎还可以(我不是Android开发人员)。忘记Request.Params
,搜索如何读取请求内容。
byte[] bitmap = GetBytes(Request.Form["IMAGE"]);
Log("HERE");
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bitmap)))
{
Log("Save 1");
image.Save(@"C:/doka/output.jpg", ImageFormat.Jpeg); // Or Png
Log("Save 2");
}
}
static byte[] GetBytes(string str)
{
Log("GetBytes 1");
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
Log("GetBytes 2");
return bytes;
}
File photo = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photo.getAbsolutePath(), options);
//selected_photo.setImageBitmap(bitmap);
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(urls[0]);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
entityBuilder.addPart("IMAGE", bab);
}
catch(Exception e){
//Log.v("Exception in Image", ""+e);
}
HttpEntity entity = entityBuilder.build();
post.setEntity(entity);
HttpResponse response = null;
try {
response = httpClient.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String sResponse;
StringBuilder s = new StringBuilder();
try {
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpFileCollection files = Request.Files;
HttpPostedFile file = files[0];
int filelength = file.ContentLength;
byte[] input = new byte[filelength];
file.InputStream.Read(input, 0, filelength);
file.SaveAs(@"C:/doka/output.jpg");