Soap request creation using ksoap2 for multilevel tags

前端 未结 1 810
醉话见心
醉话见心 2020-12-22 06:41

I want to build a soap request using KSOAP2 for android application. how to create a request for the given below soap request.



        
相关标签:
1条回答
  • 2020-12-22 07:05

    For Building soap request ..

    public String sendSoapRequest(Context c) throws Exception {
    
    
        String finalString = "Paste your whole request through which you can send request from browser sucessfully";
        Log.i("TAG", "*********************** FinalString Before "
                + FinalString);
    
    
    
        // send SOAP request
        InputStream resInputStream = sendRequest(FinalString);
    
        // create the response SOAP envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
    
        // process SOAP response
        parseResponse(resInputStream, envelope);
    
        Object bodyIn = envelope.bodyIn;
    
        SoapObject RequestSOAP = (SoapObject) envelope.bodyIn;
        String response = RequestSOAP.getProperty(0).toString();
        if (bodyIn instanceof SoapFault) {
            throw (SoapFault) bodyIn;
        }
        return response.toString();
    }
    

    calling sendRequest ..

     private InputStream sendRequest(String requestContent) throws Exception {
    
        // initialize HTTP post
        HttpPost httpPost = null;
    
        try {
            httpPost = new HttpPost(PostURL);
    
            httpPost.addHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.addHeader("SOAPAction", "Your Soap Action");
        } 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 code
            int responseStatusCode = httpResponse.getStatusLine()
                    .getStatusCode();
    
            // if the response code is not 200 - OK, or 500 - Internal error,
            // then communication error occurred
            if (responseStatusCode != 200 && responseStatusCode != 500) {
                String errorMsg = "Got SOAP response code "
                        + 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;
    }
    

    call parseResponse

      /**
     * Parses the input stream from the response into SoapEnvelope object.
     */
    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);
    
        }
    
    }
    

    call SendOrderDetails...

     private class SendOrderDetails extends AsyncTask<String, CartViewItemsBean, String> {
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected String doInBackground(String... arg) {
            String fdfd = "";
            try {
                fdfd = sendSoapRequest(getActivity());
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return fdfd;
        }
    
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
    
            Log.i("transactionresponse", result);
    
            if (!result.equalsIgnoreCase("")) {
                try {
                    helpher.deleteTotalRecord();
                    String ffsd = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + result.toString();
                    XmlToJson xmlToJson = new XmlToJson.Builder(ffsd.trim()).build();
                    JSONObject jsonObject = xmlToJson.toJson();
    
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
            } else {
    
            }
    
    
        }
    }
    

    And you have to use library XmlToJson finally you have to call this using

    new SendOrderDetails().execute();
    
    0 讨论(0)
提交回复
热议问题