Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 调用web服务时出错_Android - Fatal编程技术网

Android 调用web服务时出错

Android 调用web服务时出错,android,Android,我是安卓网络服务呼叫的新手。我正试图开发一个应用程序,通过调用soapweb服务来显示当前的温度。当我尝试运行应用程序时,我在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.SoapO

我是安卓网络服务呼叫的新手。我正试图开发一个应用程序,通过调用soapweb服务来显示当前的温度。当我尝试运行应用程序时,我在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的德克萨斯市当前温度的正确方法还是错误方法。如果可能的话,请给我一个示例代码


提前感谢。

当您尝试在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();
    }
}

}

当您尝试在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();
    }
}

}

您需要使用
线程
异步任务
来处理与网络相关的任务operations@Raghunandan,如何使用线程和异步任务?任何例子请?搜索stackoverflow你会发现许多类似的职位。您是否也将ksoap jar添加到了您的libs文件夹中?谢谢您的回复@Raghunandan,我有所有需要的.jar文件。现在,当我开始运行我的应用程序时,出现了错误09-03 03:12:22.466:E/dalvikvm(1632):找不到类“org.ksoap2.serialization.SoapObject”,引用自方法com.example.updatesnew.MainActivity$SoapAccessTask.DoinBackground您确实有JAR,但是否正确配置了它们?对于与网络相关的operations@Raghunandan,如何使用线程和异步任务?任何例子请?搜索stackoverflow你会发现许多类似的职位。您是否也将ksoap jar添加到了您的libs文件夹中?谢谢您的回复@Raghunandan,我有所有需要的.jar文件。现在,当我开始运行我的应用程序时,出现了错误09-03 03:12:22.466:E/dalvikvm(1632):找不到类“org.ksoap2.serialization.SoapObject”,引用自方法com.example.updatesnew.MainActivity$SoapAccessTask.DoinBackground您确实有JAR,但是否正确配置了它们?感谢您的回答@Emkey,我是否应该使用函数调用-new PostDataAsyncTask().execute();在onCreate()方法内部,调用setcontentView()之后?@nki:是的,您是对的。或者用任何你需要的方法。我照你说的做了。但也得到了同样的错误。09-03 05:08:22.818:E/AndroidRuntime(1832):致命异常:AsyncTask#1 09-03 05:08:22.818:E/AndroidRuntime(1832):java.lang.RuntimeException:在android.os.AsyncTask$3执行doInBackground()时发生错误09-03 05:08:22.818:E/AndroidRuntime(1832):完成(AsyncTask.java:299)09-03 05:08:22.818:E/AndroidRuntime(1832):原因:java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject位于com.example.updatesnew.MainActivity$SoapAccessTask.doInBackground(MainActivity.java:48)感谢您的回答@Emkey,我是否应该使用函数调用-new-PostDataAsyncTask().execute();在onCreate()方法内部,调用setcontentView()之后?@nki:是的,您是对的。或者用任何你需要的方法。我照你说的做了。但也得到了同样的错误。09-03 05:08:22.818:E/AndroidRuntime(1832):致命异常:AsyncTask#1 09-03 05:08:22.818:E/AndroidRuntime(1832):java.lang.RuntimeException:在android.os.AsyncTask$3执行doInBackground()时发生错误09-03 05:08:22.818:E/AndroidRuntime(1832):完成(AsyncTask.java:299)09-03 05:08:22.818:E/AndroidRuntime(1832):原因:java.lang.NoClassDefFoundError:org.ksoap2.serialization.SoapObject位于com.example.updatesnew.MainActivity$SoapAccessTask.doInBackground(MainActivity.java:48)