用于调用 Web 服务并给出“应用程序意外停止”的 Android 代码错误
我已经尝试了一段时间,但无法获得正确的输出。我面临着从 Android 代码调用 Web 服务的问题。我的 Web 服务代码 返回一个字符串,中间有分隔符“#”。现在我需要在 Android 中编写一个代码来调用 Web 服务,分隔分隔符,以便使用复选框显示实际名称,供用户选择
这是我的 Android 代码:
package com.sencide;
import java.net.SocketException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class TestApp extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/getData";
private static final String METHOD_NAME = "getData";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2/login2/Service1.asmx";
TextView tv;
boolean[] bln1=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text1);
String[] arr2= call();
boolean[] bln = {false, false, false};
bln1 = new boolean[arr2.length];
new AlertDialog.Builder(TestApp.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle("Title")
.setMultiChoiceItems(arr2,
bln,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
if(isChecked){
bln1[whichButton] = true;
}
else{
bln1[whichButton] = false;
}
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
}
public String[] call()
{
SoapPrimitive responsesData = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
responsesData = (SoapPrimitive) envelope.getResponse();
System.out.println(" --- response ---- " + responsesData);
} catch (SocketException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( " ----" + responsesData );
String serviceResponse= responsesData .toString();
String[] temp;
String delimiter = "#";
temp= serviceResponse.split(delimiter);
System.out.println( " ---- length ---- " + temp.length);
return temp;
}
}
请任何人都可以帮我解决我的问题,因为每次我运行代码时都会收到一条错误消息“应用程序意外终止”
谢谢
I have trying this for some time now but I am not able to get proper output.I am facing a problem calling the web service from my Android code. My web service code returns a string with a delimiter '#' in between. Now i need to make a code in Android that will call the web service , separate the delimiters such that the actual names are displayed using a check box for the user to select
Here is my Android code :
package com.sencide;
import java.net.SocketException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class TestApp extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/getData";
private static final String METHOD_NAME = "getData";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2/login2/Service1.asmx";
TextView tv;
boolean[] bln1=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.text1);
String[] arr2= call();
boolean[] bln = {false, false, false};
bln1 = new boolean[arr2.length];
new AlertDialog.Builder(TestApp.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle("Title")
.setMultiChoiceItems(arr2,
bln,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
if(isChecked){
bln1[whichButton] = true;
}
else{
bln1[whichButton] = false;
}
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.show();
}
public String[] call()
{
SoapPrimitive responsesData = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
responsesData = (SoapPrimitive) envelope.getResponse();
System.out.println(" --- response ---- " + responsesData);
} catch (SocketException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( " ----" + responsesData );
String serviceResponse= responsesData .toString();
String[] temp;
String delimiter = "#";
temp= serviceResponse.split(delimiter);
System.out.println( " ---- length ---- " + temp.length);
return temp;
}
}
Please can anyone help me solve my problem since everytime I run the code I get an error saying "Application terminated unexpectedly"
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在UI线程中调用Web服务,这意味着UI线程将被阻塞,直到您从Web服务获得响应。由于 Android 是移动操作系统,因此它会终止阻塞线程以提高性能。
尝试在 异步任务 中编写 Web 服务调用。使用异步任务,您可以在后台线程中运行 Web 服务调用,并且您的 UI 线程不会被阻塞。
那应该可以解决你的问题。
You are calling the web service in the UI thread, which means that the UI thread is blocked till you get the response from the web service. Since Android is a mobile OS, it terminates blocked threads to improve performance.
Try writing the web service call in an Async Task. Using an Async task you can run the web service call in a background thread and your UI thread wouldn't be blocked.
That should solve your problem.