-1
我想從android發送並從.net頁面,我可以得到字符串參數,但我有困難的時間獲取字節數組。不能轉換類型'字符串'字節[]'
ANDROID
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
byte[] bitmap = (byte[])Request.Params["IMAGE"];
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bitmap)))
{
image.Save(@"C:/output.jpg", ImageFormat.Jpeg);
}
TRY 1
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;
}
錯誤消息告訴你到底是什麼問題..你知道字符串和字節[]'甚至字符串,這之間的'差異符號'[]'一個是另一個不是的數組..你也有你的指尖的源代碼..所以'使用DEBUGGER !!' – MethodMan 2015-02-11 19:19:38
你的例外在哪裏?它在這裏:'(byte [])Request.Params [「IMAGE」];'?如果是這樣檢查:http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte – 2015-02-11 19:19:41
您將圖像寫入body( 'addBinaryBody')並嘗試從url('Request.Params')中讀取(在開發Web應用程序時,[Fiddler](http://www.telerik.com/fiddler)是一個非常好的工具) – EZI 2015-02-11 19:21:03