场景:我正在从 asp.net Web 服务下载一个文件到我的 android 应用程序中。Web 服务返回一个字节数组。我能够将文件下载到特定位置。但是当我在 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();
}
安卓代码:
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();
}
}
});
}
}