在我的应用程序中,我习惯于通过编辑文本给出一个字符串值来调用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);


        }

    }

最佳答案

您已经在AsyncResponseListener中创建了一个回调接口RegisterToServer。因此您需要通过在Activity中实现onresponse来更新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)

10-06 06:30