1

我是 Android Web 服务调用的新手。我正在尝试通过调用 SOAP Web 服务来开发一个显示当前温度的应用程序。当我尝试运行应用程序时,Logcat 中出现以下错误。

09-03 02:38:16.246: E/AndroidRuntime(1539): FATAL EXCEPTION: main
09-03 02:38:16.246: E/AndroidRuntime(1539): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
09-03 02:38:16.246: E/AndroidRuntime(1539):     at com.example.updatesnew.MainActivity.onCreate(MainActivity.java:28)

MainActivity.java

public class MainActivity extends Activity
{
    private static final String SOAP_ACTION = "http://ws.cdyne.com/WeatherWS/GetCityWeatherByZip";
    private static final String METHOD_NAME = "GetCityWeatherByZip";
    private static final String NAMESPACE ="http://ws.cdyne.com/WeatherWS/";
    private static final String URL = "http://wsf.cdyne.com/WeatherWS/Weather.asmx?op=GetCityWeatherByZIP";
    TextView tv;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           tv = (TextView) findViewById(R.id.textView01);
           SoapSerializationEnvelope soapEnvelope = null;
//Here I am getting error. 
           SoapObject Request = new SoapObject(NAMESPACE,METHOD_NAME); 

           Request.addProperty("Texas","75011");
           soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
           soapEnvelope.dotNet = true;
           soapEnvelope.setOutputSoapObject(Request);
           @SuppressWarnings("deprecation")
        AndroidHttpTransport aht=new AndroidHttpTransport(URL);
           try
           {
                aht.call(SOAP_ACTION,soapEnvelope);
                SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
                tv.setText("Status: "+resultString);
           }
           catch (Exception e)
           {
                e.printStackTrace();
           }
    }

我也有一个疑问,无论Request.addProperty("Texas","75011");是正确的方法与否,以获取邮政编码 75011 的德克萨斯城的当前温度。请指导我,如果可能的话,提供示例代码。

提前致谢。

4

1 回答 1

1

当您尝试在 Android 中对 Web 服务进行一些操作时,您必须使用 AsyncTask 来防止主线程上的网络错误,这是我在上传内容时使用的示例代码:

public class PostDataAsyncTask extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        super.onPreExecute();
        // do stuff before posting data
    }

    @Override
    protected String doInBackground(String... strings) {
        try {

            // 1 = post text data, 2 = post file
            int actionChoice = 2;

            // post a text data
            if(actionChoice==1){
                postText();
            }

            // post a file
            else{
                postFile();
            }

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String lenghtOfFile) {
        // do stuff after posting data
    }

// this will post our text data
private void postText(){
    try{
        // url where the data will be posted
        String postReceiverUrl = "http://yourdomain.com/post_data_receiver.php";
        Log.v(TAG, "postURL: " + postReceiverUrl);

        // HttpClient
        HttpClient httpClient = new DefaultHttpClient();

        // post header
        HttpPost httpPost = new HttpPost(postReceiverUrl);

        // add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("firstname", "Mike"));
        nameValuePairs.add(new BasicNameValuePair("lastname", "Dalisay"));
        nameValuePairs.add(new BasicNameValuePair("email", "mike@testmail.com"));

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // execute HTTP post request
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {

            String responseStr = EntityUtils.toString(resEntity).trim();
            Log.v(TAG, "Response: " +  responseStr);

            // you can add an if statement here and do other actions based on the response
        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

如何使用 AsyncTask:

new PostDataAsyncTask().execute();

这里给你一个参考。

于 2013-09-03T06:58:39.843 回答