为什么我的应用程序崩溃,当我尝试将一些数据发布到Web服务?

时间:2014-04-22 13:11:45

标签: android web-services android-asynctask

我正在做的是我试图从我的活动中发送一些字符串并发布到网络服务..我的代码是这样的:

approve.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//              Intent intent=new Intent(Info.this,MainActivity.class);

                 status="1";
                 ordernumber="25";
                 userid="11";
                 //Create instance for AsyncCallWS
                AsyncCallWS task = new AsyncCallWS();
                //Call execute 
                task.execute();
            }   
        });

     private class AsyncCallWS extends AsyncTask {

        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub

                loginStatus = WebService.invokeLoginWS(status,ordernumber,userid,"GetOrderData");
                return null;
            }

            protected void onPostExecute(Void result) {
                //Make Progress Bar invisible
                webservicePG.setVisibility(View.INVISIBLE);
                Intent intObj = new Intent(Info.this,PenddingOrders.class);
                //Error status is false
            }


            //Make Progress Bar visible
            protected void onPreExecute() {
                webservicePG.setVisibility(View.VISIBLE);
            }


        } 

这是我的网络服务类:

package post;


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;

public class WebService {
    //Namespace of the Webservice - can be found in WSDL
    private static String NAMESPACE = "http://service.programmerguru.com/";
    //Webservice URL - WSDL File location    
    private static String URL = "http://192.168.1.113/testOrder/OrderAndroid.asmx";//Make sure you changed IP address
    //SOAP Action URI again Namespace + Web method name
    private static String SOAP_ACTION = "http://service.programmerguru.com/";

    public static boolean invokeLoginWS(String status,String orderid,String username, String webMethName) {
        boolean loginStatus = false;
        // Create request
        SoapObject request = new SoapObject(NAMESPACE, webMethName);
        // Property which holds input parameters
        PropertyInfo statusPI = new PropertyInfo();
        PropertyInfo orderidPI = new PropertyInfo();
        PropertyInfo usernamePI = new PropertyInfo();
        // Set Username
        statusPI.setName("status");
        // Set Value
        statusPI.setValue(status);
        // Set dataType
        statusPI.setType(String.class);
        // Add the property to request object
        request.addProperty(statusPI);
        //Set Password
        orderidPI.setName("orderid");
        //Set dataType
        orderidPI.setValue(orderid);
        //Set dataType
        orderidPI.setType(String.class);
        //Add the property to request object
        request.addProperty(orderidPI);
        usernamePI.setName("username");
        //Set dataType
        usernamePI.setValue(username);
        //Set dataType
        usernamePI.setType(String.class);
        //Add the property to request object
        request.addProperty(usernamePI);


        // Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        // Set output SOAP object
        envelope.setOutputSoapObject(request);
        // Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


        try {
            // Invoke web service
            androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
            // Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            // Assign it to  boolean variable variable
            loginStatus = Boolean.parseBoolean(response.toString());

        } catch (Exception e) {
            //Assign Error Status true in static variable 'errored'

            e.printStackTrace();
        } 
        //Return booleam to calling object
        return loginStatus;
    }
}

当我按下批准按钮时我的活动崩溃了,这里是我在logcat中看到的一些内容:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.newmaamoontest.Info$AsyncCallWS.onPreExecute(Info.java:151)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)

3 个答案:

答案 0 :(得分:1)

您没有初始化webservicePG视图变量。您试图在不实例化对象的情况下调用方法。 尝试使用onCreate方法初始化和实例化webservicePG webservicePG = findViewById(R.id.your_id); 或者如果你是动态创建的 webservicePG = new View(this);

答案 1 :(得分:0)

在我看来,在您尝试设置其可见性时,尚未初始化webservicePG变量。 这是一个观点,所以你打电话给 webservicePG = findViewById(R.id.your_webservicePG_id);onCreate()onCreateView()

答案 2 :(得分:0)

更好的解决方案是移动

         status="1";
         ordernumber="25";
         userid="11";
         //Create instance for AsyncCallWS
        AsyncCallWS task = new AsyncCallWS();
        //Call execute 
        task.execute();

一种方法。在该方法中,您启动progressdialog并执行任务。对于Android,你会以你正在做的方式遇到问题。