栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无法以编程方式获得SOAP Web服务的正确响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无法以编程方式获得SOAP Web服务的正确响应

长期面对此问题后,我找到了解决方案。任何人都可以尝试一下,因为它对我有用。

我对上述问题的解决方案:

首先

txt file
{%key%}
无论您要在何处动态添加参数,我都做出了类似SOAP
Request的请求。制作此txt文件,并将其放入
raw
文件夹创建的
res
文件夹中。现在您将具有
yourfilename.txt
如下所示的外观:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://test.login/">   <soapenv:Header/>   <soapenv:Body>      <test:Login>         <!--Optional:-->         <arg0>{%arg0%}</arg0>         <!--Optional:-->         <arg1>{%arg1%}</arg1>         <!--Optional:-->         <arg2>{%arg2%}</arg2>      </test:Login>   </soapenv:Body></soapenv:Envelope>

现在,您将在Java文件中执行以下代码,以访问Right Response:

您将通过调用以下方法获得响应:

response = new sendSoapRequest(  getApplicationContext(), yourargument1, yourargument2, yourargument3);

使用以下包含的方法:

    public String sendSoapRequest(Context c, String arg0, String arg1, String arg2) throws Exception { URL = "PASTE YOUR WS URL";//"http://test.tpsynergy.com:8080/tpsynergy/services/login";        String FinalString = getStringFromRaw(c);        Log.i("TAG", "*********************** FinalString Before "     + FinalString);        FinalString = getFinalString(FinalString, arg0, arg1, arg2);        Log.i("TAG", "***********************After Finallyyy   :  FinalString "     + FinalString);        // send SOAP request        InputStream responseIs = sendRequest(FinalString);        // create the response SOAP envelope        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(     SoapEnvelope.VER11);        // process SOAP response        parseResponse(responseIs, envelope);        Object bodyIn = envelope.bodyIn;        SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;        String response = resultsRequestSOAP.getProperty(0).toString();        Log.i(" Login Webservice Response",     "Responce ---->" + response.toString());        //        if (bodyIn instanceof SoapFault) { throw (SoapFault) bodyIn;        }        return response.toString();    }    public String getFinalString(String ABC, String arg0, String arg1, String arg2) {        // parse the template and replace all keywords        HashMap<String, String> hashMap = new HashMap<String, String>();        hashMap.put("arg0", arg0);        hashMap.put("arg1", arg1);        hashMap.put("arg2", arg2);        StringBuffer sb = new StringBuffer();        try { // find all keywords Pattern patern = Pattern.compile("\{%(.*?)%\}"); Matcher matcher = patern.matcher(ABC); while (matcher.find()) {     String keyName = matcher.group(1);     Log.i("LOG_TAG", "Key Name" + keyName);     String keyValue = hashMap.get(keyName);     if (keyValue == null) {         keyValue = "";     }     // replace the key with value     matcher.appendReplacement(sb, keyValue); } matcher.appendTail(sb); // return the final string return sb.toString();        } catch (Throwable e) { Log.e("LOG_TAG", "Error parsing template", e); return null;        }    }    private String getStringFromRaw(Context c) throws IOException {        Resources r = c.getResources();        InputStream is;        if (MainActivity.PRODUCTION) { Log.i(TAG, "Inside Production Raw File"); is = r.openRawResource(R.raw.logininfoproduction);        } else { Log.i(TAG, "Inside Test Raw File"); is = r.openRawResource(R.raw.logininfotest);        }        String statesText = convertStreamToString(is);        is.close();        return statesText;    }    private String convertStreamToString(InputStream is) throws IOException {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        int i = is.read();        while (i != -1) { baos.write(i); i = is.read();        }        return baos.toString();    }        private InputStream sendRequest(String requestContent) throws Exception {        // initialize HTTP post        HttpPost httpPost = null;        try { httpPost = new HttpPost(URL); httpPost.addHeader("Accept-Encoding", "gzip,deflate"); httpPost.addHeader("Content-Type", "text/xml;charset=UTF-8"); httpPost.addHeader("SOAPAction", """");        } catch (Throwable e) { Log.e("LOG_TAG", "Error initializing HTTP post for SOAP request", e); // throw e;        }        // load content to be sent        try { HttpEntity postEntity = new StringEntity(requestContent); httpPost.setEntity(postEntity);        } catch (UnsupportedEncodingException e) { Log.e("LOG_TAG",         "Unsupported ensoding of content for SOAP request", e); throw e;        }        // send request        HttpResponse httpResponse = null;        HttpClient httpClient = new DefaultHttpClient();        try { httpResponse = httpClient.execute(httpPost);        } catch (Throwable e) { Log.e("LOG_TAG", "Error sending SOAP request", e); // throw e;        }        // get SOAP response        try { // get response pre int responseStatusCode = httpResponse.getStatusLine()         .getStatusCode(); // if the response pre is not 200 - OK, or 500 - Internal error, // then communication error occurred if (responseStatusCode != 200 && responseStatusCode != 500) {     String errorMsg = "Got SOAP response pre "  + responseStatusCode + " "  + httpResponse.getStatusLine().getReasonPhrase();     // ... } // get the response content HttpEntity httpEntity = httpResponse.getEntity(); InputStream is = httpEntity.getContent(); return is;        } catch (Throwable e) { Log.e("LOG_TAG", "Error getting SOAP response", e); // throw e;        }        return null;    }        private void parseResponse(InputStream is, SoapEnvelope envelope) throws Exception {        try { XmlPullParser xp = new KXmlParser(); xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); xp.setInput(is, "UTF-8"); envelope.parse(xp);        } catch (Throwable e) { Log.e("LOG_TAG", "Error reading/parsing SOAP response", e);        }    }

现在,您不必担心ComplexType Request参数或任何其他事情。因为您正在传递与XML相同的请求,例如SOAPUI工具正在传递。

现在,您具有所需的正确响应。

希望它对其他人也有用。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373247.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号