Android HttpGet request -
this question has answer here:
i tried json string server receive error when want connect server:
10-07 10:57:47.410: e/androidruntime(11050): fatal exception: main 10-07 10:57:47.410: e/androidruntime(11050): java.lang.runtimeexception: unable start activity componentinfo{com.example.stampp/com.example.stampp.ui.mainactivity}: android.os.networkonmainthreadexception 10-07 10:57:47.410: e/androidruntime(11050): @ android.app.activitythread.performlaunchactivity(activitythread.java:1970) 10-07 10:57:47.410: e/androidruntime(11050): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1995) 10-07 10:57:47.410: e/androidruntime(11050): @ android.app.activitythread.access$600(activitythread.java:128) 10-07 10:57:47.410: e/androidruntime(11050): @ android.app.activitythread$h.handlemessage(activitythread.java:1161) 10-07 10:57:47.410: e/androidruntime(11050): @ android.os.handler.dispatchmessage(handler.java:99) 10-07 10:57:47.410: e/androidruntime(11050): @ android.os.looper.loop(looper.java:137) 10-07 10:57:47.410: e/androidruntime(11050): @ android.app.activitythread.main(activitythread.java:4517) 10-07 10:57:47.410: e/androidruntime(11050): @ java.lang.reflect.method.invokenative(native method) 10-07 10:57:47.410: e/androidruntime(11050): @ java.lang.reflect.method.invoke(method.java:511) 10-07 10:57:47.410: e/androidruntime(11050): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:993) 10-07 10:57:47.410: e/androidruntime(11050): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:760) 10-07 10:57:47.410: e/androidruntime(11050): @ dalvik.system.nativestart.main(native method) 10-07 10:57:47.410: e/androidruntime(11050): caused by: android.os.networkonmainthreadexception 10-07 10:57:47.410: e/androidruntime(11050): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1099) 10-07 10:57:47.410: e/androidruntime(11050): @ libcore.io.blockguardos.connect(blockguardos.java:84) 10-07 10:57:47.410: e/androidruntime(11050): @ libcore.io.iobridge.connecterrno(iobridge.java:127) 10-07 10:57:47.410: e/androidruntime(11050): @ libcore.io.iobridge.connect(iobridge.java:112) 10-07 10:57:47.410: e/androidruntime(11050): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 10-07 10:57:47.410: e/androidruntime(11050): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:459) 10-07 10:57:47.410: e/androidruntime(11050): @ java.net.socket.connect(socket.java:848) 10-07 10:57:47.410: e/androidruntime(11050): @ org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory.java:119) 10-07 10:57:47.410: e/androidruntime(11050): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:144) 10-07 10:57:47.410: e/androidruntime(11050): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 10-07 10:57:47.410: e/androidruntime(11050): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 10-07 10:57:47.410: e/androidruntime(11050): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 10-07 10:57:47.410: e/androidruntime(11050): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:674) 10-07 10:57:47.410: e/androidruntime(11050): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:511) 10-07 10:57:47.410: e/androidruntime(11050): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:489) 10-07 10:57:47.410: e/androidruntime(11050): @ com.example.stampp.services.configurateapi.connectapi.postauthorization(connectapi.java:51) 10-07 10:57:47.410: e/androidruntime(11050): @ com.example.stampp.ui.mainactivity.oncreate(mainactivity.java:61) 10-07 10:57:47.410: e/androidruntime(11050): @ android.app.activity.performcreate(activity.java:4470) 10-07 10:57:47.410: e/androidruntime(11050): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1053) 10-07 10:57:47.410: e/androidruntime(11050): @ android.app.activitythread.performlaunchactivity(activitythread.java:1934) 10-07 10:57:47.410: e/androidruntime(11050): ... 11 more
this code:
public void getauthorization(string password, string user) { string responsestring="empty response api"; string userpassword = user+":"+password; try { httpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget(_url); httpget.setheader("authorization", "basic "+base64.encode(userpassword.getbytes(),base64.no_wrap)); httpresponse httpresponse = httpclient.execute(httpget); statusline statusline = httpresponse.getstatusline(); if(statusline.getstatuscode() == httpstatus.sc_ok){ bytearrayoutputstream out = new bytearrayoutputstream(); httpresponse.getentity().writeto(out); out.close(); responsestring = out.tostring(); } else{ //closes connection. httpresponse.getentity().getcontent().close(); throw new ioexception(statusline.getreasonphrase()); } }catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } }
can me?
since android 3.0 cannot use network connection on main thread. better create asyntask
doinbackground
execute httpget
.
Comments
Post a Comment