需要使用webservice传输.apk文件
我已将我的 apk 文件转换为字节数组并使用 Web 服务发送它,如下
[WebMethod]
public byte[] GetApkFile(string deviceID)
{
try
{
string path = ServiceHelper.GetTempFilePath();
string fileName = path + "\\VersionUpdate.apk";
FileStream fileStream = File.OpenRead(fileName);
return ConvertStreamToByteBuffer(fileStream);
}
catch (Exception ex)
{
throw ex;
}
}
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
所示 我已在 Android 应用程序中使用 ksoap 协议作为数组字节使用 Web 服务,如下所示
public void DownloadApkFile(String serverIPAddress,
String deviceId) {
String SOAP_ACTION = "http://VisionEPODWebService/GetApkFile";
String OPERATION_NAME = "GetApkFile";
String WSDL_TARGET_NAMESPACE = "http://VisionEPODWebService/";
String SOAP_ADDRESS = "";
SOAP_ADDRESS = "http://" + serverIPAddress
+ "/VisionEPODWebService/SystemData.asmx";
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
new MarshalBase64().register(envelope);
envelope.encodingStyle = SoapEnvelope.ENC;
request.addProperty("deviceID", deviceId);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try {
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
byte[] b=response.toString().getBytes();
String fileName = "/sdcard/" + "VersionUpdate" + ".apk";
FileOutputStream fileOuputStream =
new FileOutputStream(fileName);
fileOuputStream.write(b);
fileOuputStream.close();
}
catch (Exception exception) {
exception.toString();
}
问题是我在转换后没有获得确切的 apk 文件字节数组[]返回文件。
请有人检查一下代码并告诉我这是否有任何错误。
我需要将转换后的 byte[] apk 文件恢复为 SD 卡中的 .apk 文件进行安装。
I have converted my apk file into byte array and send it using webservice as follows
[WebMethod]
public byte[] GetApkFile(string deviceID)
{
try
{
string path = ServiceHelper.GetTempFilePath();
string fileName = path + "\\VersionUpdate.apk";
FileStream fileStream = File.OpenRead(fileName);
return ConvertStreamToByteBuffer(fileStream);
}
catch (Exception ex)
{
throw ex;
}
}
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}
I have consumed the web service using ksoap protocol in my android application as bytes of array as given below
public void DownloadApkFile(String serverIPAddress,
String deviceId) {
String SOAP_ACTION = "http://VisionEPODWebService/GetApkFile";
String OPERATION_NAME = "GetApkFile";
String WSDL_TARGET_NAMESPACE = "http://VisionEPODWebService/";
String SOAP_ADDRESS = "";
SOAP_ADDRESS = "http://" + serverIPAddress
+ "/VisionEPODWebService/SystemData.asmx";
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER10);
new MarshalBase64().register(envelope);
envelope.encodingStyle = SoapEnvelope.ENC;
request.addProperty("deviceID", deviceId);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
try {
httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
byte[] b=response.toString().getBytes();
String fileName = "/sdcard/" + "VersionUpdate" + ".apk";
FileOutputStream fileOuputStream =
new FileOutputStream(fileName);
fileOuputStream.write(b);
fileOuputStream.close();
}
catch (Exception exception) {
exception.toString();
}
The problem is that I am not getting the exact apk file after converting byte array[] back to file.
Will anyone please review the code and please tell me is there any bug in this.
My need to get the converted byte[] apk file back to .apk file in the sdcard for installation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
response.toString()
可能不是您的 APK 的字符串表示形式。请尝试以下操作:
response.toString()
is likely not a String representation of your APK.Try the following: