Android KSOAP2 HelloWorld web服务失败
好的,我在网上搜索了StackOverflow这个答案,但我没有运气。。。我只是想做最基本的web服务功能…Android KSOAP2 HelloWorld web服务失败,android,web-services,ksoap2,Android,Web Services,Ksoap2,好的,我在网上搜索了StackOverflow这个答案,但我没有运气。。。我只是想做最基本的web服务功能…HelloWorld我有一个按钮,你点击它时,它会将文本设置为web服务中函数返回的任何内容。。这是我的密码: public class MainActivity extends ActionBarActivity { private final String NAMESPACE = "http://tempuri.org/"; private final String URL = "h
HelloWorld
我有一个按钮,你点击它时,它会将文本设置为web服务中函数返回的任何内容。。这是我的密码:
public class MainActivity extends ActionBarActivity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://myInfo/App/Service1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private final String METHOD_NAME = "HelloWorld";
private String TAG = "SOAP";
private static String message;
Button b;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
b = (Button) findViewById(R.id.button1);
//Button Click Listener
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Create instance for AsyncCallWS
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
//If text control is empty
}
});
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
Log.i(TAG, "doInBackground");
getMessage();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
tv.setText(message);
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
tv.setText("Rendering...");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
}
public void getMessage() {
//Create request
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//Property which holds input parameters
/*PropertyInfo messagePI = new PropertyInfo();
//Set Name
messagePI.setName("Practice");
//Set Value
messagePI.setValue(messagePI);
//Set dataType
messagePI.setType(double.class);
//Add the property to request object
request.addProperty(messagePI);*/
//Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
//Set output SOAP object
envelope.setOutputSoapObject(request);
//Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
//Invoke web service
Log.i(TAG, "Point A");
androidHttpTransport.debug=true;
androidHttpTransport.call(SOAP_ACTION, envelope);
//androidHttpTransport.responseDump;
//Get the response
//SoapObject result = (SoapObject) envelope.bodyIn;
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Assign it to fahren static variable
message = response.toString();
/*if(message !=null){
tv.setText(meesage.getProperty(0).toString());
}*/
//tv.setText("" + results);
} catch (Exception e) {
tv.setText(e.getMessage());
e.printStackTrace();
}
}
当我刚刚运行程序时,会显示LogCat
错误,但当我实际执行程序时,会收到此消息
org.xmlpull.v1.XmlPullParserException: expected: START_TAG
在这一行:
}捕获(异常e){
如果有任何帮助,我将不胜感激……我已经在这个问题上陷入了一段时间,很遗憾……:(我认为问题就在这里:因为现在你在后台线程(doInBackground())中,并试图设置文本(tv.setText)。只有主线程可以这样做。 删除“tv.setText(e.getMessage());”语句,它应该可以工作
catch (Exception e) {
tv.setText(e.getMessage());
e.printStackTrace();
}
这将解决以下问题:
原因:android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图
然后您可以尝试解决另一个问题
检查以下链接是否有助于您进行其他修复:
是的,您不能在后台调用setText()。只需从getMessage()方法返回值,并在doInBackground()中返回相同的值。您将在onPostExecute()中获得该值,您可以通过调用setText()进行设置。所以您说的是remove
catch(异常e){tv.setText(e.getMessage());e.printStackTrace();}
?不,只需删除tv.setText(e.getMessage());由于您在后台线程中,正在尝试更新UIAhh gotcha!您是对的,它不再崩溃,但仍然没有返回我想要的信息…检查您现在发送的链接在跟踪链接后,我仍然无法从web服务获取数据…:(
catch (Exception e) {
tv.setText(e.getMessage());
e.printStackTrace();
}