在我的应用程序中,我习惯通过编辑文本给出一个字符串值来调用web服务。我可以从web服务获得一个响应字符串。我可以将这个响应字符串从RegisterToServer类带到我的MainActivity。我已经尝试了但是无法得到任何结果。我是我的MainActivity.Java和RegisterToServer类的代码
MainActivity.Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
T1 = (TextView)findViewById(R.id.textView1);
E1 = (EditText)findViewById(R.id.editText1);
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String PlatNo = E1.getText().toString();
if (!PlatNo.equals("")){
RegisterToServer regServer= new RegisterToServer(null);
/*User userInfo= new User();*/
/* userInfo.setPlateNo(PlateNo);*/
regServer.execute(PlatNo);
Bundle extras = getIntent().getExtras();
if(extras!=null){
String RTS=extras.getString("RS");
E1.setText(RTS);
}
}else{
Toast.makeText( MainActivity.this, "Enter String", Toast.LENGTH_LONG).show();
}
}
});
}}
RegisterToServer.java
public class RegisterToServer extends AsyncTask<String, Void, Void> {
String SOAP_ACTION1 = "http://tempuri.org/GetRoadTax";
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME1 = "GetRoadTax";
String URL="http://192.168.0.102/RoadTax/WebService1.asmx";
private String TAG = "GetRoadTax";
String responseString;
String responseStatus = "SUCCESS";
Context context;
AsyncResponseListener responseListener;
public interface AsyncResponseListener {
public void onresponse(String status, String jsonData);
}
public RegisterToServer(Context c) {
responseListener = (AsyncResponseListener) c;
context = c;
}
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground");
Log.i(TAG, params[0]);
RegToServer(params[0]);
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
// Display the progress bar
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
private void RegToServer(String PlatNo) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
request.addProperty("PlatNo", PlatNo);
Log.i("TAG","Inside regtoserver before webservice call"+PlatNo);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION1, envelope);
Object response = (Object) envelope.getResponse();
responseString=response.toString();
Log.i("GET ROAD TAX", "Websevice called");
Log.i("Response String from WebService", responseString);
Intent intent=new Intent();
intent.putExtra("RS",responseString);
} catch (Exception e) {
e.printStackTrace();
responseString = e.toString();
Log.i("GET ROAD TAX", responseString);
}
}
答案 0 :(得分:1)
您已在AsyncResponseListener
中创建了回调接口RegisterToServer
。因此,您需要在onresponse
方法中通过在Activity中实施AsyncResponseListener
来更新EditText中的响应:
1。在活动中实施AsyncResponseListener
:
public class MainActivity extends Activity implement
RegisterToServer.AsyncResponseListener{
....
}
2。在onresponse
方法中更新EditText中的文字:
@Override
public Void onresponse(String status,String jsonData) {
//update text here in E1
}
3。:在MainActivity.this
类构造函数中传递RegisterToServer
:
RegisterToServer regServer= new RegisterToServer(MainActivity.this);
4。从responseListener.onresponse
致电onPostExecute
:
responseListener.onresponse("status".responseString)
答案 1 :(得分:0)
您可以使用LocalBroadcastReceiver。
在活动的OnResume中注册广播并在onPause中取消注册。