1

我在我的 android 应用程序中使用 ksoap2 api 作为参考,将数据从我的 android 应用程序存储到远程 SQL Server 数据库。这个想法是保存用户数据,这些数据是为构建用户配置文件而收集的信息。我在下面使用了这个内部doInBackground()方法 :AsyncTask

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
request.addProperty("userName",username.getText().toString());
request.addProperty("eamil",email.getText().toString());
request.addProperty("gender",gender.getSelectedItem().toString());
request.addProperty("country",country.getSelectedItem().toString());
request.addProperty("about",about.getText().toString() );
request.addProperty("pic",byteArray);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,20000);
                androidHttpTransport.call(SOAP_ACTION1, envelope);
                if (envelope.bodyIn instanceof SoapFault) {
                    String str= ((SoapFault) envelope.bodyIn).faultstring;
                    Log.i("fault", str);
                } else {
                    SoapObject result = (SoapObject)envelope.bodyIn;
                    if(result != null)
                    {
                          message=result.getProperty(0).toString();
                    }
                }
} catch (Exception e) {
 e.printStackTrace();
}
        return message;

问题是,当我添加时 request.addProperty("pic",byteArray); 收到一个错误,指出无法序列化 Ksoap2,但是当我将类型byteArray从类型byte[ ]更改为string正确执行的请求并将数据保存在我的数据库中时。这是我的网络服务的截图

Public Function AddUser(userName As String, email As String, gender As String, country As String, about As String, pic As Byte()) as String
// Some code to add data to databae 
Return "you are done"
 End Function

任何有关此问题的帮助将不胜感激

问候

4

1 回答 1

0

我想我知道如何解决上述问题,如下所示:

我没有向 Web 服务发送一个字节 [],而是改变了我的想法来发送如下构建的字符串:

Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    String strBase64=Base64.encodeToString(byteArray, 0);

然后我strBase64使用request.addProperty("pic",strBase64);

然后要检索该字符串并再次使其成为图片,我只需从远程数据库中检索该字符串,然后执行以下操作:

byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);

strBase64我从远程数据库中检索到的字符串在哪里。

于 2013-03-18T21:40:30.317 回答