Android SOAP客户端代码示例

MainActivity.java

package com.sherin.soapdemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;


public class MainActivity extends AppCompatActivity {
    public static final String API_BASE_URL = "http://www.dneonline.com/calculator.asmx";
    public static final String SOAP_ACTION = "http://tempuri.org/Multiply";
    public static final String METHOD_NAME = "Multiply";
    public static final String NAMESPACE = "http://tempuri.org/";

    private static final String tag = MainActivity.class.getSimpleName();

    private ProgressDialog pDialog;
    private EditText etConvert;
    private Button btnConvert;

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

        etConvert = (EditText) findViewById(R.id.etConvert);
        btnConvert = (Button) findViewById(R.id.btnConvert);
        btnConvert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etConvert.getText() != null && etConvert.getText().toString().length() > 0) {
                    String toConvert = etConvert.getText().toString();

                    if (isConnectedToInternet(MainActivity.this)) {
                        // Using KSoap2 library
                        new AsyncTaskBraille().execute(toConvert);

                    } else {
                        showSnackBar(MainActivity.this, "Please check your internet connection.");
                    }
                } else {
                    showSnackBar(MainActivity.this, "Please enter atleast 1 digit.");
                }
            }
        });

        showSnackBar(this, "Welcome");
    }

    protected void hideProgressDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
        }
    }

    private class AsyncTaskBraille extends AsyncTask {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = ProgressDialog.show(MainActivity.this,
                    "SOAP Demo",
                    "Processing...",
                    true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i(tag, "API call completed");
            hideProgressDialog();

            if (hasValue(s)) {
                Log.i(tag, "Processing ...");
                Toast.makeText(MainActivity.this, "Result = " + s, Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        protected String doInBackground(String... strings) {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("intA", strings[0]);
            request.addProperty("intB", "20");

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.implicitTypes = true;
            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;

            HttpTransportSE httpTransportSE = new HttpTransportSE(API_BASE_URL);
            httpTransportSE.debug = true;

            try {
                httpTransportSE.call(SOAP_ACTION, envelope);
            } catch (IOException | XmlPullParserException e) {
                e.printStackTrace();
            }

            Object result = null;
            try {
                result = envelope.getResponse();
                Log.i(tag, String.valueOf(result));

            } catch (SoapFault soapFault) {
                soapFault.printStackTrace();
            }

            return result != null ? String.valueOf(result) : "";
        }
    }

    public static boolean isConnectedToInternet(Context _context) {
        ConnectivityManager connectivity = (ConnectivityManager) _context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean hasValue(String data) {
        if (data == null || data.length() == 0) {
            return false;
        } else {
            if (data.equalsIgnoreCase("null") || data.equalsIgnoreCase("nil")) {
                return false;
            }
        }
        return true;
    }

    public static void showSnackBar(Activity context, String message) {
        Snackbar snack = Snackbar.make(context.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
        View view = snack.getView();
        TextView tv = (TextView) view.findViewById(com.google.android.material.R.id.snackbar_text);
        if (tv != null) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            }
            tv.setGravity(Gravity.CENTER_HORIZONTAL);
        }
        snack.show();
    }
}

activity_main.xml




    

    

AndroidManifest.xml




    
    

    
        
            
                

                
            
        
    

Android SOAP客户端代码示例_第1张图片

 

你可能感兴趣的:(android,java,apache)