简体   繁体   中英

How can I call a HTTPS soap web service in Android

I am trying to create an android application in which I am calling a HTTPS server. I know how to call HTTP, but I am unable to call HTTPS server. My code is shown below:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    Spinner spinner;
    Button send;
    EditText number, comments;
    String dropdown;
    TextView feedbackText;
    private static final String[] paths = {"About Test Center", "About Mobile App"};

    final private static String URL = "https://122.180.25.2/TempWS/WebServices/WebService1.asmx";
    final private static String METHOD_NAME = "RegisterComplaint";
    final private static String NAMESPACE = "https://122.180.25.2/";
    final private static String SOAP_ACTION = "https://122.180.25.2/RegisterComplaint";

    @Override
    protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Feedback");
        send = (Button) findViewById(R.id.button7);
        number = (EditText) findViewById(R.id.editText);
        comments = (EditText) findViewById(R.id.editText2);
        feedbackText = (TextView) findViewById(R.id.textView2);

        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item, paths);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AsyncFeedback().execute();
            }
        });
    }

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

    switch (position) {
        case 0:
            dropdown = "About Test Center";
            break;
        case 1:
            dropdown = "About Mobile App";
            break;
    }
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}

public class AsyncFeedback extends AsyncTask<Void, Void, String> {

    String resp;
    String Number = number.getText().toString();
    String Comments = comments.getText().toString();

    @Override
    protected String doInBackground(Void... params) {

        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //request.addProperty("topic",dropdown);
            request.addProperty("sender", Number);
            request.addProperty("topic", dropdown);
            request.addProperty("CompDetails", Comments);
            //request.addProperty("details",Comments);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;
            try {
                HttpTransportSE transportSE = new HttpTransportSE(URL);
                transportSE.call(SOAP_ACTION, envelope);

                SoapObject result = (SoapObject) envelope.bodyIn;

                resp = result.toString();

            } catch (SocketTimeoutException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp;
    }

    public void onPostExecute(String result) {
        dropdown = null;
        number.setText("");
        comments.setText("");
        feedbackText.setText(result);
    }
}
}

The logcat is shown below:

Click here to view the image of logcat

Any help would be appreciated. I want to make this code work(how to call HTTPS). Thank You!!

@Nair123 This is a REST based answer, but this is the code snippet I used to get it to trust all certificates

X509TrustManager trust;
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
        trust = new X509TrustManager() {
             @Override
             public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}

             @Override
             public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}

             @Override
             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                 return new java.security.cert.X509Certificate[]{};
             }
        }
};

// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

Add the said SSLContext to your SSLSocketFactory such that .sslSocketFactory(sslContext.getSocketFactory(), trust)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM