asp.net - Connecting Android to web service -
this question has answer here:
i'm making android application supposed connect made localhost web service. things aren't going well. have following code:
public void proben() {
string soapmsg="<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:envelope xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:body>"+ "<selectvezba xmlns=\"http://tempuri.org/\">"+ "<ime>" + "a" + "</ime>" + "</selectvezba>" + "</soap:body>" + "</soap:envelope>"; log.d("where", "before others"); textview t = (textview)findviewbyid(r.id.textview2); httpparams httpparameters = new basichttpparams(); // set timeout in milliseconds until connection established. int timeoutconnection = 15000; httpconnectionparams.setconnectiontimeout(httpparameters,timeoutconnection); // set default socket timeout (so_timeout) // in milliseconds timeout waiting data. int timeoutsocket = 35000; httpconnectionparams.setsotimeout(httpparameters, timeoutsocket); defaulthttpclient httpclient = new defaulthttpclient(httpparameters); httppost httppost = new httppost(constants.service_location); httppost.setheader("content-type", constants.content_type); httppost.setheader("soapaction", constants.get_selectvezba); log.d("where","before try"); httpentity entity; try { entity = new stringentity(soapmsg, http.utf_8); httppost.setentity(entity); } catch (unsupportedencodingexception e) { // todo auto-generated catch block log.d("where", "in catch 1"); e.printstacktrace(); } httpresponse response; try { log.d("where", "in catch 0"); response = httpclient.execute(httppost); log.d("where","1"); if(response == null) { log.d("tag","response e null"); } httpentity r_entity = response.getentity(); // response header[] headers = response.getallheaders(); if (r_entity != null) { string str_entity = entityutils.tostring(r_entity); log.d("response",str_entity); } } catch (clientprotocolexception e) { log.d("where", "in catch 2"); // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block log.d("where", "in catch 3"); e.printstacktrace(); }// calling server //parsing irrelevant question parsersaxlista parser=new parsersaxlista("nov.xml"); listview listview1 = (listview)findviewbyid(r.id.listview1); arraylist<entitet> nova=parser.objekti(); arraylist<string> lista=new arraylist<string>(); (entitet en : nova) { lista.add(en.tostring()); } arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, lista); listview1.setadapter(adapter);
}
the constants used defined follows:
public final static string service_location = "http://10.0.2.2:63919/servis.asmx"; public final static string content_type = "text/xml; charset=utf-8"; public final static string get_selectvezba = "http://tempuri.org/selectvezba";
in logcat, "in catch 0" , "in catch 3" tells me there's exception coming line:
response = httpclient.execute(httppost);
how fix this? read elsewhere i'm supposed add internet permission in manifest, upon writing:
<uses-permission android:name="android.permission.internet"></uses-permission>
in manifest, program started crashing immediately.
any appreciated. sorry such long code in question.
- edit
logcat:
07-11 09:10:54.224: d/androidruntime(1355): >>>>>> androidruntime start com.android.internal.os.runtimeinit <<<<<< 07-11 09:10:54.224: d/androidruntime(1355): checkjni on 07-11 09:10:54.344: d/dalvikvm(1355): trying load lib libjavacore.so 0x0 07-11 09:10:54.354: d/dalvikvm(1355): added shared lib libjavacore.so 0x0 07-11 09:10:54.394: d/dalvikvm(1355): trying load lib libnativehelper.so 0x0 07-11 09:10:54.394: d/dalvikvm(1355): added shared lib libnativehelper.so 0x0 07-11 09:10:55.244: d/androidruntime(1355): calling main entry com.android.commands.pm.pm 07-11 09:10:55.285: d/androidruntime(1355): shutting down vm 07-11 09:10:55.304: d/dalvikvm(1355): gc_concurrent freed 100k, 19% free 470k/576k, paused 1ms+5ms, total 16ms 07-11 09:10:55.304: d/dalvikvm(1355): debugger has detached; object registry had 1 entries 07-11 09:10:56.984: d/androidruntime(1368): >>>>>> androidruntime start com.android.internal.os.runtimeinit <<<<<< 07-11 09:10:56.984: d/androidruntime(1368): checkjni on 07-11 09:10:57.329: d/dalvikvm(1368): trying load lib libjavacore.so 0x0 07-11 09:10:57.354: d/dalvikvm(1368): added shared lib libjavacore.so 0x0 07-11 09:10:57.474: d/dalvikvm(1368): trying load lib libnativehelper.so 0x0 07-11 09:10:57.474: d/dalvikvm(1368): added shared lib libnativehelper.so 0x0 07-11 09:10:58.914: d/androidruntime(1368): calling main entry com.android.commands.am.am 07-11 09:10:58.924: d/dalvikvm(1368): note: class landroid/app/activitymanagernative; has 156 unimplemented (abstract) methods 07-11 09:10:58.964: i/activitymanager(269): start u0 {act=android.intent.action.main cat=[android.intent.category.launcher] flg=0x10000000 cmp=com.example.anotherstart/.mainactivity} pid 1368 07-11 09:10:58.994: w/windowmanager(269): failure taking screenshot (328x583) layer 21010 07-11 09:10:59.054: d/androidruntime(1368): shutting down vm 07-11 09:10:59.084: d/dalvikvm(1368): gc_concurrent freed 101k, 17% free 501k/604k, paused 1ms+1ms, total 29ms 07-11 09:10:59.104: d/jdwp(1368): got wake-up signal, bailing out of select 07-11 09:10:59.104: d/dalvikvm(1368): debugger has detached; object registry had 1 entries 07-11 09:10:59.234: d/dalvikvm(1380): not late-enabling checkjni (already on) 07-11 09:10:59.244: i/activitymanager(269): start proc com.example.anotherstart activity com.example.anotherstart/.mainactivity: pid=1380 uid=10048 gids={50048, 3003, 1028} 07-11 09:10:59.644: e/surfaceflinger(36): ro.sf.lcd_density must defined build property 07-11 09:10:59.994: e/trace(1380): error opening trace file: no such file or directory (2) 07-11 09:11:02.296: e/surfaceflinger(36): ro.sf.lcd_density must defined build property 07-11 09:11:02.684: d/gralloc_goldfish(1380): emulator without gpu emulation detected. 07-11 09:11:03.244: i/activitymanager(269): displayed com.example.anotherstart/.mainactivity: +4s57ms (total +13m39s598ms) 07-11 09:11:11.134: i/activitymanager(269): start u0 {cmp=com.example.anotherstart/.addhrana} pid 1380 07-11 09:11:11.134: w/windowmanager(269): failure taking screenshot (328x583) layer 21015 07-11 09:11:11.654: d/kade(1380): pred site drugi 07-11 09:11:11.654: d/kaj sum(1380): pred try 07-11 09:11:11.674: d/kade(1380): vo catch 0 07-11 09:11:11.844: d/androidruntime(1380): shutting down vm 07-11 09:11:11.864: w/dalvikvm(1380): threadid=1: thread exiting uncaught exception (group=0x2bd39930) 07-11 09:11:11.934: e/androidruntime(1380): fatal exception: main 07-11 09:11:11.934: e/androidruntime(1380): java.lang.runtimeexception: unable start activity componentinfo{com.example.anotherstart/com.example.anotherstart.addhrana}: android.os.networkonmainthreadexception 07-11 09:11:11.934: e/androidruntime(1380): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 07-11 09:11:11.934: e/androidruntime(1380): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 07-11 09:11:11.934: e/androidruntime(1380): @ android.app.activitythread.access$600(activitythread.java:141) 07-11 09:11:11.934: e/androidruntime(1380): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 07-11 09:11:11.934: e/androidruntime(1380): @ android.os.handler.dispatchmessage(handler.java:99) 07-11 09:11:11.934: e/androidruntime(1380): @ android.os.looper.loop(looper.java:137) 07-11 09:11:11.934: e/androidruntime(1380): @ android.app.activitythread.main(activitythread.java:5039) 07-11 09:11:11.934: e/androidruntime(1380): @ java.lang.reflect.method.invokenative(native method) 07-11 09:11:11.934: e/androidruntime(1380): @ java.lang.reflect.method.invoke(method.java:511) 07-11 09:11:11.934: e/androidruntime(1380): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-11 09:11:11.934: e/androidruntime(1380): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 07-11 09:11:11.934: e/androidruntime(1380): @ dalvik.system.nativestart.main(native method) 07-11 09:11:11.934: e/androidruntime(1380): caused by: android.os.networkonmainthreadexception 07-11 09:11:11.934: e/androidruntime(1380): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 07-11 09:11:11.934: e/androidruntime(1380): @ libcore.io.blockguardos.connect(blockguardos.java:84) 07-11 09:11:11.934: e/androidruntime(1380): @ libcore.io.iobridge.connecterrno(iobridge.java:144) 07-11 09:11:11.934: e/androidruntime(1380): @ libcore.io.iobridge.connect(iobridge.java:112) 07-11 09:11:11.934: e/androidruntime(1380): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 07-11 09:11:11.934: e/androidruntime(1380): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:459) 07-11 09:11:11.934: e/androidruntime(1380): @ java.net.socket.connect(socket.java:842) 07-11 09:11:11.934: e/androidruntime(1380): @ org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory.java:119) 07-11 09:11:11.934: e/androidruntime(1380): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:144) 07-11 09:11:11.934: e/androidruntime(1380): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 07-11 09:11:11.934: e/androidruntime(1380): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 07-11 09:11:11.934: e/androidruntime(1380): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 07-11 09:11:11.934: e/androidruntime(1380): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 07-11 09:11:11.934: e/androidruntime(1380): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 07-11 09:11:11.934: e/androidruntime(1380): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 07-11 09:11:11.934: e/androidruntime(1380): @ com.example.anotherstart.addhrana.proben(addhrana.java:92) 07-11 09:11:11.934: e/androidruntime(1380): @ com.example.anotherstart.addhrana.oncreate(addhrana.java:144) 07-11 09:11:11.934: e/androidruntime(1380): @ android.app.activity.performcreate(activity.java:5104) 07-11 09:11:11.934: e/androidruntime(1380): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 07-11 09:11:11.934: e/androidruntime(1380): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 07-11 09:11:11.934: e/androidruntime(1380): ... 11 more 07-11 09:11:12.034: d/dalvikvm(1380): gc_concurrent freed 198k, 3% free 9137k/9380k, paused 75ms+16ms, total 321ms 07-11 09:11:12.034: d/dalvikvm(1380): wait_for_concurrent_gc blocked 60ms 07-11 09:11:12.064: w/activitymanager(269): force finishing activity com.example.anotherstart/.addhrana 07-11 09:11:12.084: w/activitymanager(269): force finishing activity com.example.anotherstart/.mainactivity 07-11 09:11:12.494: e/surfaceflinger(36): ro.sf.lcd_density must defined build property 07-11 09:11:12.594: w/activitymanager(269): activity pause timeout activityrecord{2cae4aa0 u0 com.example.anotherstart/.addhrana} 07-11 09:11:13.184: i/choreographer(269): skipped 45 frames! application may doing work on main thread. 07-11 09:11:13.274: i/mipsassembler(36): generated scanline__00000077:03515104_00008001_00000000 [206 ipp] (245 ins) @ [0x2d401970:0x2d401d44] in 0 ns 07-11 09:11:13.484: i/choreographer(1145): skipped 63 frames! application may doing work on main thread. 07-11 09:11:13.794: e/surfaceflinger(36): ro.sf.lcd_density must defined build property 07-11 09:11:14.431: i/choreographer(1145): skipped 97 frames! application may doing work on main thread. 07-11 09:11:26.971: w/activitymanager(269): activity destroy timeout activityrecord{2cb65278 u0 com.example.anotherstart/.mainactivity} 07-11 09:11:26.971: w/activitymanager(269): activity destroy timeout activityrecord{2cae4aa0 u0 com.example.anotherstart/.addhrana}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.internet"></uses-permission> <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.anotherstart.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.example.anotherstart.pregled" android:label="@string/title_activity_pregled" > </activity> <activity android:name="com.example.anotherstart.addhrana" android:label="@string/title_activity_add_hrana" > </activity> <activity android:name="com.example.anotherstart.addvezba" android:label="@string/title_activity_add_vezba" > </activity> </application>
looking @ logcat output can see that:
(...) 07-11 09:11:11.934: e/androidruntime(1380): caused by: android.os.networkonmainthreadexception (...)
at line.
this meet @selvin's comment possible duplicate of "android.os.networkonmainthreadexception".
as answer tells: asynctask solution
you should doing network work in asynctask object , pass final result (or progressresult) , asynctask object, called on ui thread (where handle ui elements/events/answers).
don't hesitate take @ : asynctask documentation .
Comments
Post a Comment