1

hi i am trying to parse soap .net xml service using "http". but using ksoap2 i am able to parse it. but i want to parse it using "http". it is possible or not i dont know. so help me to solve this issue. here is my activity class with ksoap .

    public class Caly_act extends Activity {



    private final String NAMESPACEreg2 = "http://tempuri.org/";
    private final String URLreg2 = "http://pqr.com/androidservice.asmx";
    //  private final String URLreg = "http://192.168.0.173:88/AndroidService.asmx";
    private final String ACTIONReferees2  = "http://tempuri.org/ColMas";
    private final String METHOD_NAMEReferees2="ColMas";


    private String res;

    Spinner name_prefix,referance_select;
    String strng_nameprefix,strng_referanse_select;
    TextView t;
    String ref_name;
    String res_Referees;
    int Total_Referees;



    String[] data,data1,data2,data3;

    public static ArrayList<Colordata> colorlist;

    String response,shape="BR",clarity="IF",colordt="D",cents="",str,percnt="0";
    EditText pricelist,carat,price,total;
    ArrayList<String> per =new ArrayList<>();
    float priceval= (float) 0.0;
    int perval=0;
    int per1;
    String caratval;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caly_act);

            new Colorascy().execute();
    }


    private class Colorascy extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {

            SoapObject Request2 =new SoapObject(NAMESPACEreg,METHOD_NAMEReferees);
            SoapSerializationEnvelope soapEnvelope1 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope1.dotNet=true;
            soapEnvelope1.setOutputSoapObject(Request2);

            AndroidHttpTransport aht1 =new AndroidHttpTransport(URLreg);
            try{
                aht1.call(ACTIONReferees,soapEnvelope1);
                SoapPrimitive res_Referees = (SoapPrimitive)soapEnvelope1.getResponse(); //get the response from your webservice
                response= res_Referees.toString();
            }

            catch(Exception exp){

            }
            try{
                JSONArray array =  new JSONArray(response);
                colorlist = new ArrayList<Colordata>();
                JSONArray jsonArray = new JSONArray(response);
                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject c = jsonArray.getJSONObject(i);
                    colorlist.add(new Colordata(c.getString("RAPCOL"), c.getString("SNM")));
                }

            }
            catch(Exception e){
                e.printStackTrace();
            }

            return response;
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

        }
    }
}

here is my async class with http method.

private class Colorascy extends AsyncTask<String, String, String> {
   HttpHandler parser = new HttpHandler();
        response = parser.makeGetServiceCall( StaticDataUtility.Server_URL,+""+StaticDataUtility.color,parser.GET);

        response = response.substring(response.indexOf("["),response.lastIndexOf("]")+1);

        if (response != null) {
            colorlist = new ArrayList<Colordata>();
            try {

                JSONArray jsonArray = new JSONArray(response);
                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject c = jsonArray.getJSONObject(i);
                    colorlist.add(new Colordata(c.getString("RAPCOL"), c.getString("SNM")));
                }

            } catch (final JSONException e) {
              //  e.printStackTrace();
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });
            }
        }else {
          //  Log.e("responsenulll",response.toString());
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });
        }
        return response;
}

here is my httphandler class.

    public class HttpHandler {
    static String response = null;
    public static final int GET = 1;
    public static final int POST = 2;
    public static final int SOAP = 3;
    HttpURLConnection connection = null;
    BufferedReader reader = null;
    URL url = null;

    public HttpHandler(){

    }

    public String makeGetServiceCall(String reqUrl, int method) {

        if (method == GET) {
            try {
                url = new URL(reqUrl);
                connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(10000);
                connection.setConnectTimeout(50000);
                connection.setRequestMethod("GET");
                connection.connect();

                int status = connection.getResponseCode();

                if (status == 200) {
                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line = "";
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                    response = builder.toString();
                }else {
                    reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                    Log.e("reader",reader.toString());
                }
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response;
    }



    public String makePostServiceCall(String reqUrl, Uri.Builder params, int method) {

        if (method == POST) {
            try {
                url = new URL(reqUrl);
                connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(10000);
                connection.setConnectTimeout(50000);
                connection.setRequestMethod("POST");
                connection.setDoInput(true);
                connection.setDoOutput(true);

                String query = params.build().getEncodedQuery();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
                writer.write(query);
                writer.flush();
                writer.close();

                connection.connect();

                int status = connection.getResponseCode();

                if (status == 200) {

                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line = "";

                    while ((line = reader.readLine()) != null) {

                        builder.append(line);
                    }

                    response = builder.toString();

                }else {
                    reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                    Log.e("reader",connection.getErrorStream().toString());
                }


            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response;

    }


}

0 Answers0