2

Web服务内容如下:

[WebMethod]
public string SendGcm(String serviceKey,String registrationId ,string message) {
    WebClient wc=new WebClient();
    wc.Headers.Add("Authorization", "key=" + serviceKey);
    NameValueCollection nameValues=new NameValueCollection
        {
            {"registration_id", registrationId},
            {"collapse_key", Guid.NewGuid().ToString()},
            {"data.payload", message}
        };

    var resp=wc.UploadValues("https://android.googleapis.com/gcm/send",
                nameValues);

    var respMessage = Encoding.Default.GetString(resp);
            return respMessage;
}

安卓代码:

package com.example.insertpro;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String SOAP_ACTION = "http://tempuri.org/SendGcm";
    private static final String OPERATION_NAME = "SendGcm";
    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
    private static final String SOAP_ADDRESS = "http://*******/Demo/service.asmx";
    String s="";
    boolean flag=true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new UploadTask().execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private class UploadTask extends AsyncTask<Void,Void, Void> {

        private ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setTitle("eAbsenceTracker");
            dialog.setMessage("Please Wait..");
            dialog.setIndeterminate(false);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setProgress(0);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
            request.addProperty("serviceKey","*****************);
            request.addProperty("registrationId","***************");
            request.addProperty("message","Hi");

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

            try {

                httpTransport.call(SOAP_ACTION, envelope);
                Object response = envelope.getResponse();

                s=response.toString();
            } catch (Exception exception) {
                Log.e("Shyam","exception",exception);
                // Toast.makeText(getApplicationContext(),"In
    Exception"+exception,Toast.LENGTH_LONG).show();
                                   flag=false;
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            dialog.dismiss();

            if(flag==false) {
                Toast.makeText(getApplicationContext(), "In Exception",Toast.LENGTH_LONG).show();
            } else {
                if(s.trim().equals("false")) {
                    Toast.makeText(MainActivity.this, "Not inserted",Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "Successfully inserted"+s,Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}

例外 :

Exception SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable
to process   request. ---> The underlying connection was
    closed: An unexpected error occurred on a send. ---> Unable to read
    data from the transport connection: An existing connection was
    forcibly closed by the remote host. ---> An existing connection was
    forcibly closed by the remote host' faultactor: 'null' detail:
    org.kxml2.kdom.Node@4052ff00

请帮助我,我在服务器端尝试了很多东西,但没有奏效。

4

0 回答 0