java - How get information from Web page (Android)? -



java - How get information from Web page (Android)? -

i create android application must receive info page http://example.com/get.php?id=1. utilize next method:

private string getdata() { httpclient httpclient = new defaulthttpclient(); httpcontext localcontext = new basichttpcontext(); // !!! application stops here httpget httpget = new httpget("http://example.com/get.php?id=1"); httpresponse response = null; seek { response = httpclient.execute(httpget, localcontext); } grab (clientprotocolexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } string result = ""; bufferedreader reader = null; seek { reader = new bufferedreader( new inputstreamreader( response.getentity().getcontent() ) ); } grab (illegalstateexception e) { // todo auto-generated grab block e.printstacktrace(); } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } string line = null; seek { while ((line = reader.readline()) != null){ result += line + "\n"; } } grab (ioexception e) { // todo auto-generated grab block e.printstacktrace(); } // have whole html loaded on result variable homecoming result; }

i added permission androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.repetitor_ent_free" android:versioncode="1" android:versionname="1.0" > <uses-permission android:name="android.permission.internet" />

the problem when run application on device error: unexpected stop application myapp (process com.android.myapp). please seek again.

you got android.os.networkonmainthreadexception, if improve solution seek utilize asynctask. more threads @ 'processes , threads' (please, read guide avoid issue in future).

for illustration seek 1 of these solutions:

1) utilize roman's reply , add together this:

final stringbuilder sb = ... ; final textview tv = ... ; tv.post(new runnable() { public void run() { tv.settext(sb.tostring()); } });

or utilize activity method:

runonuithread(new runnable() { public void run() { final stringbuilder sb = ... ; final textview tv = ... ; tv.settext(sb.tostring()); } });

2) utilize asynctask:

new asynctask<void, void, void> () { protected void doinbackground(void... none) { // // code // homecoming null; } protected void onprogressupdate(void... none) {} protected void onpostexecute(void none) { // utilize set text final stringbuilder sb = ... ; final textview tv = ... ; tv.settext(sb.tostring()); } }.execute();

java android http httpcontext

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -