使用Ksoap2发送ArrayList

时间:2017-08-22 04:05:40

标签: java android arraylist ksoap2

我正在尝试发送一个arraylist,这是一个编码字符串并获得

java.lang.RuntimeException:无法序列化:[abc.docx,def.docx]

这就是我将它转换为字节数组并传递它的方式:

 public static String convertFileToByteArray(File f) {
    byte[] byteArray = null;
    try {
        InputStream inputStream = new FileInputStream(f);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024 * 11];
        int bytesRead = 0;
        while ((bytesRead = inputStream.read(b)) != -1) {
            bos.write(b, 0, bytesRead);
        }
        byteArray = bos.toByteArray();

        Log.e("Byte array", ">" + byteArray);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(byteArray, Base64.NO_WRAP);
}

将其添加到ArrayList,如下所示:

ArrayList<String> encodedlist = new ArrayList<String>();
encodedString = convertFileToByteArray(mySelectedFile);
encodedlist.add(encodedDocument);

这是我将其发送到服务器端的代码:

 public static boolean invokeUploadMultipleDocuments(String RandomFolder, ArrayList FileNames, String webMethodName) {
    boolean UploadStatus = false;
    // Create request
    SoapObject request = new SoapObject(NAMESPACE, webMethodName);
    // Property which holds input parameters
    PropertyInfo filenamePI = new PropertyInfo();

    // Set Filenames
    filenamePI.setName("FileNames");
    // Set Value
    filenamePI.setValue(FileNames);
    // Set dataType
    filenamePI.setType(ArrayList.class);
    // Add the property to request object
    request.addProperty(filenamePI);

    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        // Invoke web service
        androidHttpTransport.call(SOAP_ACTION+webMethodName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to  boolean variable variable
        UploadDocumentStatus = Boolean.parseBoolean(response.toString());
    } catch (Exception e) {
        //Assign Error Status true in static variable 'errored'
        MainActivity.errored = true;
        e.printStackTrace();
    }
    //Return booleam to calling object
    return UploadStatus;
}

1 个答案:

答案 0 :(得分:0)

您可以使用GSON库依赖项 在build.gradle文件中

dependencies {
     compile 'com.google.code.gson:gson:2.2.4'
}

然后使用以下代码

List<List> encodedlist = Arrays.asList(Arrays.asList("one", "two","three"), 
                                       Arrays.asList("four", "five","six"));

String levelPatternGson = new Gson().toJson(encodedlist);
invokeUploadMultipleDocuments(levelPatternGson);

现在将代码发送到服务器端

public SoapObject invokeUploadMultipleDocuments(String levelPatternGson){ 
         SoapObject request = new SoapObject(NAMESPACE, webMethodName);
         SoapObject WebRq = new SoapObject("", "PropertyInfo");
         WebRq.addProperty("FileNames",FileNames);
         request.addSoapObject(WebRq);
         return request;
      }

注意:我没有尝试过自己,但可能会这样做。