场景:我正在从asp.net web服务下载文件到我的android应用程序。 Web Service返回一个字节数组。我可以将文件下载到特定位置。但是当我在Android中运行它时,它会给出消息:
“解析包时出现问题”
这是我的网络服务代码和Android代码:
网络服务代码:
[WebMethod]
public byte[] GetApkFile(string deviceID)
{
try
{
string path = "D:\\Users\\CHAITANYA\\Documents";
string fileName = path + "\\sample.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代码:
package com.example.dwnload;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class FileDownloadActivity extends Activity
{
/** Called when the activity is first created. */
TextView tv;
Button dwnload;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dwnload=(Button)this.findViewById(R.id.button1);
tv=(TextView)this.findViewById(R.id.textView1);
dwnload.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String deviceId = TelephonyMgr.getDeviceId();
String SOAP_ACTION = "http://tempuri.org/GetApkFile";
String METHOD_NAME = "GetApkFile";
String NAMESPACE = "http://tempuri.org/";
final String URL = "http://10.0.2.2/Dashboard/Service.asmx";
SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//new MarshalBase64().register(envelope);
envelope.encodingStyle = SoapEnvelope.ENC;
request.addProperty("deviceID", deviceId);
//tv.setText(deviceId.toString());
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(URL);
try
{
httpTransport.call(SOAP_ACTION, envelope);
//SoapObject response = (SoapObject)envelope.getResponse();
Object response = envelope.getResponse();
byte[] b=response.toString().getBytes();
String s = new String(b);
try
{
String filePath = Environment.getExternalStorageDirectory()+ "/myappdir/" + "sample.apk" ;
File file = new File(filePath);
file.getParentFile().mkdirs();
file.createNewFile();
BufferedOutputStream objectOut = new BufferedOutputStream(new FileOutputStream(file));
objectOut.write(b);
objectOut.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception exception)
{
exception.toString();
}
}
});
}
}
答案 0 :(得分:1)
你在android上有一条线
byte[] b=response.toString().getBytes();
应替换为
byte[] b=Base64.decode(response.toString());
因为soap envelop会将其编码为< base64Binary>
并且不要忘记import org.kobjects.base64.Base64;
刚刚找到关于.net部分的内容。您的文件读取方法无法正确传输文件... ReadByte的内容我使用Read并传递数组(作为参考),然后返回具有正确数据的数组:
[WebMethod]
public byte[] GetApk()
{
string file = "file name goes here";
FileStream stream = File.OpenRead(file);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
return buffer;
}
为我工作...现在我的应用程序可以自动更新;) 我希望它对你也有用
答案 1 :(得分:0)
我建议您按照一些调试步骤进行操作。
首先验证您的网络服务是否按预期工作 您可以通过编写基于.net的客户端来执行此操作,该客户端将下载您的文件并将其写入磁盘。您可以进行二进制比较,看看您是否正在获取目标文件。
如果您确实按预期获得了该文件,那么您的Web服务正常运行。
现在将此文件与使用android下载的文件进行比较。
这可以帮助您隔离错误。