gpt4 book ai didi

android - 调用网络服务时出错

转载 作者:行者123 更新时间:2023-11-30 03:23:57 25 4
gpt4 key购买 nike

我是 Android 网络服务调用的新手。我正在尝试开发一个通过调用 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)

主 Activity .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();
}
}

}

如何使用 AsyncTask:

new PostDataAsyncTask().execute();

Here's a reference for you.

关于android - 调用网络服务时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18585872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com