2013-03-17 64 views
1

我在我的android應用程序中使用kso​​ap2 api作爲參考,將數據從我的android應用程序存儲到遠程SQL服務器數據庫。這個想法是保存作爲收集信息的用戶數據來構建用戶配置文件。我在AsyncTask如下使用該內部doInBackground()方法:使用kso​​ap2將Byte []保存到遠程數據庫中?

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請求正確執行並保存在我的數據庫中的數據。下面是從我的web

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 

關於這個問題的任何幫助snipshote將完全理解

問候

回答

0

我想我弄清楚如何解決上面我的問題說,這將是如下:

而不是發送一個byte []對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); 

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

然後檢索該字符串,並使其爲圖片再次我只是從我的遠程數據庫retrive的字符串,然後做如下發送strBase64給我的web服務作爲字符串:

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

其中strBase64是我從遠程數據庫中檢索到的字符串。