シナリオ: asp.net Web サービスから Android アプリケーションにファイルをダウンロードしています。Web サービスはバイト配列を返します。ファイルを特定の場所にダウンロードできます。しかし、Androidで実行すると、次のメッセージが表示されます。
「パッケージの解析に問題があります」
これが私のWebサービスコードとAndroidコードです:
Web サービス コード:
[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();
}
}
});
}
}