我正在尝试使用以xsd:string响应的Web服务填充android listview。
我有以下代码,并且已经向清单添加了Internet权限。
但是我得到了一个我根本不理解的错误。在代码下你可以找到我的logcat。
希望有人可以给我一些建议。
谢谢!
public class ActividadPrincipal extends Activity {
final String NAMESPACE = "http://android.trucka.com/";
final String URL = "http://192.168.2.105/SumadorWS/ServicioAndroid.asmx";
final String SOAP_ACTION = "http://android.trucka.com/InsertarMacro";
final String METHOD_NAME = "InsertarMacro";
private SoapObject request = null;
private SoapSerializationEnvelope envelope = null;
private SoapPrimitive resultRequestsSOAP = null;
Gson gson;
ListView lsvAndroidOS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actividad_principal);
setTitle("Directorio");
request = new SoapObject(NAMESPACE, METHOD_NAME);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE transporte = new HttpTransportSE(URL);
try{
transporte.call(SOAP_ACTION,envelope);
resultRequestsSOAP =(SoapPrimitive)envelope.getResponse();
}catch(IOException e){
e.printStackTrace();
}catch(XmlPullParserException e){
e.printStackTrace();
}
String strJSON = resultRequestsSOAP.toString();
crearLista(strJSON);
}
private void crearLista(String strJSON) {
gson = new Gson();
Type lstT = new TypeToken< ArrayList>(){}.getType();
ArrayList arrListAOS= new ArrayList();
arrListAOS = gson.fromJson(strJSON, lstT);
lsvAndroidOS.setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,arrListAOS));
}
}
logcat的:
06-22 12:27:58.960: E/AndroidRuntime(7825): FATAL EXCEPTION: main
06-22 12:27:58.960: E/AndroidRuntime(7825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.trucka.directorio/com.android.trucka.directorio.ActividadPrincipal}: android.os.NetworkOnMainThreadException
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2085)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2110)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.app.ActivityThread.access$600(ActivityThread.java:138)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.os.Looper.loop(Looper.java:137)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.app.ActivityThread.main(ActivityThread.java:4950)
06-22 12:27:58.960: E/AndroidRuntime(7825): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 12:27:58.960: E/AndroidRuntime(7825): at java.lang.reflect.Method.invoke(Method.java:511)
06-22 12:27:58.960: E/AndroidRuntime(7825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
06-22 12:27:58.960: E/AndroidRuntime(7825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
06-22 12:27:58.960: E/AndroidRuntime(7825): at dalvik.system.NativeStart.main(Native Method)
06-22 12:27:58.960: E/AndroidRuntime(7825): Caused by: android.os.NetworkOnMainThreadException
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.io.IoBridge.connect(IoBridge.java:112)
06-22 12:27:58.960: E/AndroidRuntime(7825): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-22 12:27:58.960: E/AndroidRuntime(7825): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
06-22 12:27:58.960: E/AndroidRuntime(7825): at java.net.Socket.connect(Socket.java:832)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
06-22 12:27:58.960: E/AndroidRuntime(7825): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
06-22 12:27:58.960: E/AndroidRuntime(7825): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
06-22 12:27:58.960: E/AndroidRuntime(7825): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
06-22 12:27:58.960: E/AndroidRuntime(7825): at com.android.trucka.directorio.ActividadPrincipal.onCreate(ActividadPrincipal.java:53)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.app.Activity.performCreate(Activity.java:5275)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
06-22 12:27:58.960: E/AndroidRuntime(7825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)