试图用|拆分字符串字符

时间:2016-02-07 00:54:36

标签: java android string ksoap

也许是因为我不是为了开发android - 但我不明白我添加参数的两种方式之间的区别。

String []不是字符串吗?

例如,如果我在下面运行我的空白点击。 Web服务按预期工作

public void onClick(View v) {
    new Thread() {
        @Override
        public void run() {//Create request
            try {


                //start SoapObject
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                /*List<String> ArgumentPipeValue = new ArrayList<String>();
                ArgumentPipeValue.add("_caseId|apples");
                for (String arg : ArgumentPipeValue) {
                    String[] splitArgs = arg.split("|");
                     request.addProperty(splitArgs[0],splitArgs[1]);;


                    }*/
                request.addProperty("_caseId", "apples");
                //create the envelope
                SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
                //Needed to make the internet call
                HttpTransportSE androidHttpTransport = getHttpTransportSE();
                //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);

                //SoapObject soapResponse = (SoapObject) envelope.getResponse();
                SoapPrimitive results = (SoapPrimitive)envelope.getResponse();




            } catch (Exception e) {
                e.printStackTrace();
                Log.w("myApp", e.getMessage());
                Log.w("myApp", e.getCause());

            }


        }
    }.start();
}

但是如果我按照下面的方式运行网络服务,我会收到错误Unexpected token (position:TEXT Bad Request@1:12 in java.io.InputStreamReader@3709d6f6)

 public void onClick(View v) {
    new Thread() {
        @Override
        public void run() {//Create request
            try {


                //start SoapObject
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                List<String> ArgumentPipeValue = new ArrayList<String>();
                ArgumentPipeValue.add("_caseId|apples");
                for (String arg : ArgumentPipeValue) {
                    String[] splitArgs = arg.split("|");
                     request.addProperty(splitArgs[0],splitArgs[1]);


                    }
                //request.addProperty("_caseId", "apples");
                //create the envelope
                SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
                //Needed to make the internet call
                HttpTransportSE androidHttpTransport = getHttpTransportSE();
                //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION, envelope);

                //SoapObject soapResponse = (SoapObject) envelope.getResponse();
                SoapPrimitive results = (SoapPrimitive)envelope.getResponse();




            } catch (Exception e) {
                e.printStackTrace();
                Log.w("myApp", e.getMessage());
                Log.w("myApp", e.getCause());

            }


        }
    }.start();
}

唯一的区别是我通过request.addProperty("_caseId", "apples");而非request.addProperty(splitArgs[0],splitArgs[1]);

添加了该属性

我错过了什么?为什么会这样?

2 个答案:

答案 0 :(得分:0)

在Java中,String[] 一个String(但是,调用String[int]会产生String)。但是,管道是一个特殊的正则表达式字符。你需要在你的分裂中逃脱它。像,

String[] splitArgs = arg.split("\\|");

答案 1 :(得分:0)

String[] split = argString.split("\\|");

然后你可以做

for(String s : split){
   System.out.println(s);
}