【问题标题】:Trying to split a string with the | character [duplicate]试图用 | 分割字符串字符[重复]
【发布时间】:2016-02-07 00:54:36
【问题描述】:

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

String[] 不是字符串吗?

例如,如果我在下面运行我的 void onclick。网络服务按预期工作

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();
}

但是,如果我按以下方式运行 Web 服务,则会收到错误 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]);添加属性

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

【问题讨论】:

  • 数组不是字符串,不是

标签: java android string ksoap


【解决方案1】:

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

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

【讨论】:

  • 对。它在空字符串或空字符串上拆分而不转义
【解决方案2】:
String[] split = argString.split("\\|");

你可以这样做

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

【讨论】:

    猜你喜欢
    • 2014-02-26
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多