使用AsyncTask调用Soap Web服务

时间:2018-02-24 07:36:26

标签: android web-services android-asynctask ksoap2

在Android中使用kso​​ap调用Web服务时遇到一些问题... 我是android新手...... 我浏览了整个互联网,但我没有得到任何关于它的信息...... 实际上我想将一些数据发送到Web服务并从中获取一些数据以便在textView上显示。我的Web服务以json数组的形式发送数据...... 这是我的肥皂要求类:

public class SoapRequest {

public SoapRequest(){}

public String callNewsList(String language, int page, String search){
    SoapObject request = new SoapObject(ServiceAPI.WSDL_TARGET_NAMESPACE, ServiceAPI.OPERATION_NAME);
    PropertyInfo propertyInfo = new PropertyInfo();
    propertyInfo.setName("lng");
    propertyInfo.setValue(language);
    propertyInfo.setType(String.class);
    request.addProperty(propertyInfo);

    propertyInfo = new PropertyInfo();
    propertyInfo.setName("page");
    propertyInfo.setValue(page);
    propertyInfo.setType(Integer.class);
    request.addProperty(propertyInfo);

    propertyInfo = new PropertyInfo();
    propertyInfo.setName("srch");
    propertyInfo.setValue(search);
    propertyInfo.setType(String.class);
    request.addProperty(propertyInfo);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;

    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransportSE = new HttpTransportSE(ServiceAPI.SOAP_ADDRESS);
    Object response = null;

    try {
        httpTransportSE.call(ServiceAPI.SOAP_ACTION, envelope);
        response = envelope.getResponse();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
    return response.toString();
}

} 这是我的asynctask:

public class MyAsync extends AsyncTask<Void, Void, Void>{
    String res = "";
    String language, search;
    int page;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Toast.makeText(MainActivity.this, "Loading Data...", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        SoapRequest soapRequest = new SoapRequest();
        language = "en";
        page = 1;
        search = "";
        res = soapRequest.callNewsList(language, page, search);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        try {
            JSONArray jsonArray = new JSONArray(res);
            for (int i = 0; i<res.length(); i++){
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);

                String ncode = jsonObject.getString("ncode");
                String nimage = jsonObject.getString("nimage");
                String ndate = jsonObject.getString("ndate");
                String ntitle = jsonObject.getString("ntitle");

                jsonResponse += "Code: " + ncode + "\n";
                jsonResponse += "Image: " + nimage + "\n";
                jsonResponse += "Date: " + ndate + "\n";
                jsonResponse += "Title: " + ntitle + "\n";
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        super.onPostExecute(aVoid);
    }
}

我得到了这个:

java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:309)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                  at java.lang.Thread.run(Thread.java:818)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                  at com.androidcopilot.webservice.SoapRequest.callNewsList(SoapRequest.java:55)
                  at com.androidcopilot.webservice.MainActivity$MyAsync.doInBackground(MainActivity.java:61)
                  at com.androidcopilot.webservice.MainActivity$MyAsync.doInBackground(MainActivity.java:43)
                  at android.os.AsyncTask$2.call(AsyncTask.java:295)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                  at java.lang.Thread.run(Thread.java:818) 

感谢您的帮助...

0 个答案:

没有答案