Java 将edittext值从android传递到web服务
我想将编辑文本值从android传递给web服务,但我不能每次都返回空值。 这是我的简单web服务Java 将edittext值从android传递到web服务,java,android,Java,Android,我想将编辑文本值从android传递给web服务,但我不能每次都返回空值。 这是我的简单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
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");
}
}
}
您正在使用
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
}
然后在AsyncTask类中
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground: barge is " + params[0]);
testMyProps(params[0]);
}
我想当你实例化
AsyncCallWS
时,你必须以params
的形式发送barge
是barge=null
的值吗?是的,编辑文本值似乎不发送。在测试方法中,你正在返回null…并且是空的..是的,我能做什么?你能帮我更多吗,我真的不明白,我如何才能将驳船名称作为参数发送,您是否有may me代码,谢谢您在比较字符串时,您必须使用equals()
方法,而不是=代码>运算符:)替换此et.getText().toString()!=“
withet.getText().toString().equals(”
感谢您的帮助但它不起作用我收到错误,我将编辑错误可能是您有什么建议,谢谢确定。强制停止应用程序。在doInBackground()testMyProps(驳船)中注释该行;看看会发生什么。您应该只看到Log.i(标记“doInBackground:barge is”+params[0]);然后从异步任务中返回。如果发生这种情况,那么testMyProps()方法中就存在一些问题。我得到了一些类似doInbacground的东西:驳船是阿伦(我填写的名字),然后12-11 09:59:41.180:D/dalvikvm(1212):GC_为所有释放的200K,11%释放的2819K/3136K,暂停103ms,总共103ms 12-11 09:59:41.190:I/编舞(1212):跳过了31帧!应用程序可能在其主线程上做了太多工作。12-11 09:59:41.300:I/PGGURU(1212):onPostExecute
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground: barge is " + params[0]);
testMyProps(params[0]);
}