简体   繁体   English

将edittext值从android传递到Web服务

[英]Pass edittext value from android to web services

I want to pass edit text value from android to web services, but i can not do that every time it returns a null value. 我想将编辑文本值从android传递到Web服务,但是每次返回空值时我都不能这样做。 This is my simple web services. 这是我简单的Web服务。

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;
 }
}

and android code; 和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");
        }

    }

}

It returns First name null; 返回名字为空;

Please help i think i cannot pass the value to web services. 请帮助我,我无法将价值传递给Web服务。 Thanks a lot. 非常感谢。

Iam getting this error, 我收到这个错误,

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

You are setting barge with the EditText value but you are not sending it inside AsyncTask 您正在使用EditText值设置barge ,但没有在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
}

And then in your AsyncTask Class 然后在您的AsyncTask类中

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

I think when you instantiate AsyncCallWS , you will have to send the barge in as params . 我认为当你实例AsyncCallWS ,你将不得不送barge作为params

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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