android中onPostExcecute方法的空指针异常

时间:2015-02-02 10:57:57

标签: android web-services

在我的项目中,我使用AsyncTask来调用web服务。当我尝试通过传递字符串值来调用Web服务时,我得到了空指针异常。我可以克服这个异常。我不知道我的代码

ValidateActivationCode.java

public class ValidateActivationCode extends AsyncTask<String, Void, Void>  {

    String SOAP_ACTION1 = "http://tempuri.org/ValidateActivationCode";
    String NAMESPACE = "http://tempuri.org/";
    String METHOD_NAME1 = "ValidateActivationCode";


    String URL="http://myasp.net/webservice2.asmx";

    private String TAG = "VALIDATING CODE";

    String responseString;
    String responseStatus = "SUCCESS";
    Context context;
    AsyncResponseListener responseListener;


    public interface AsyncResponseListener {
        public void onresponse(String status, String jsonData);
    }

    public ValidateActivationCode(Context c) {
        responseListener = (AsyncResponseListener) c;
        context = c;
    }


    @Override
    protected Void doInBackground(String... params) {
        Log.i(TAG, "doInBackground");

        RegToServer(params[0]);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "onPostExecute");
        responseListener.onresponse("status",responseString);



    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");

    }

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

        private void RegToServer(String Code) {



        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

        request.addProperty("Code", Code);

        Log.i("TAG","Inside regtoserver before webservice call "+Code);

        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(TAG,responseString );

            Log.i(TAG, "Websevice called");

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

            Log.i(TAG, "FAILED");


        }

    }

    }

logcat的enter image description here

1 个答案:

答案 0 :(得分:1)

实际上请发布您的堆栈跟踪。

仍然通过快速查看代码,空指针的可能区域可以是:     对象响应=(对象)envelope.getResponse();         responseString = response.toString();

请检查响应是否为空? 即使用这个:

Object response = (Object) envelope.getResponse();
    if(response!=null)
        responseString=response.toString();