将edittext值从android传递到Web服务

时间:2013-12-11 14:13:41

标签: java android

我想将编辑文本值从android传递给Web服务,但每次返回null值时我都不能这样做。 这是我简单的网络服务。

package org.me.mongodb;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

@WebService(serviceName = "Simple")
@Stateless()

public class Simple {

 public String testMyProps(@WebParam(name = "name")String fname){
  return "First Name : "+fname;
 }
}

和android代码;

package com.prgguru.android;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.webserviceactivity.R;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    private static final String SOAP_ACTION = "";
    private static final String METHOD_NAME = "testMyProps";
    private static final String NAMESPACE = "http://mongodb.me.org/";
    private static final String URL = "http://10.0.2.2:8080/Simple/Simple?WSDL";
    private String TAG = "PGGURU";
    private static String barge="";
    private static String info;
    Button  b;
    TextView tv;
    EditText et;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        et = (EditText) findViewById(R.id.editText1);

        tv = (TextView) findViewById(R.id.tv_result);
        //Button to trigger web service invocation
        b = (Button) findViewById(R.id.button1);
        //Button Click Listener
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Check if barge text control is not empty
                if (et.getText().length() != 0 && et.getText().toString() != "") {
                    //Get the text control value
                    barge = et.getText().toString();
                    //Create instance for AsyncCallWS
                    AsyncCallWS task = new AsyncCallWS();
                    //Call execute 
                    task.execute();

                //If text control is empty
                } else {
                    tv.setText("Please enter Barge Name");
                }
            }
        });
    }

    public void testMyProps(String fname) {

        //Create request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        //Property which holds input parameters
        PropertyInfo getinfo = new PropertyInfo();
        //Set Name
        getinfo.setName("arg0");
        //Set Value
        getinfo.setValue(fname);
        //Set dataType
        getinfo.setType(String.class);
        //Add the property to request object
        request.addProperty(getinfo);
        //Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        //Set output SOAP object
        envelope.setOutputSoapObject(request);
        //Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            //Invole web service
            androidHttpTransport.call(SOAP_ACTION,  envelope);
            //Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            //Assign it to info static variable
            info = response.toString();

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

    private class AsyncCallWS extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            Log.i(TAG, "doInBackground");
            testMyProps(barge);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.i(TAG, "onPostExecute");
            tv.setText(info);
        }

        @Override
        protected void onPreExecute() {
            Log.i(TAG, "onPreExecute");
            tv.setText("Getting...");
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            Log.i(TAG, "onProgressUpdate");
        }

    }

}

返回名字null;

请帮助我认为我无法将价值传递给网络服务。 非常感谢。

我收到此错误,

12-11 09:45:53.291: E/AndroidRuntime(1920): FATAL EXCEPTION: main
12-11 09:45:53.291: E/AndroidRuntime(1920): java.lang.IllegalStateException: Cannot execute task: the task is already running.
12-11 09:45:53.291: E/AndroidRuntime(1920):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:575)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at android.os.AsyncTask.execute(AsyncTask.java:534)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at com.prgguru.android.MainActivity$1.onClick(MainActivity.java:57)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at android.view.View.performClick(View.java:4240)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at android.view.View$PerformClick.run(View.java:17721)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at android.os.Handler.handleCallback(Handler.java:730)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at android.os.Looper.loop(Looper.java:137)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at java.lang.reflect.Method.invoke(Method.java:525)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-11 09:45:53.291: E/AndroidRuntime(1920):     at dalvik.system.NativeStart.main(Native Method)
12-11 09:45:55.031: I/Process(1920): Sending signal. PID: 1920 SIG: 9

1 个答案:

答案 0 :(得分:0)

您使用barge值设置EditText,但未将其发送到AsyncTask

barge = et.getText().toString();
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();

if(barge != null && !barge.equals("")) { // if barge is not an empty string and not null
    task.execute(barge);
    // return null; i don't think u need this return statement
}

然后在你的AsyncTask类

@Override
protected Void doInBackground(String... params) {
    Log.i(TAG, "doInBackground: barge is " + params[0]);
    testMyProps(params[0]);
}

我认为当您实例化AsyncCallWS时,您必须将barge作为params发送。