Getting Website data on android app -
Getting Website data on android app -
i trying obtain info of website in android app. tried next code, application launches , simple activity_main.xml displayed loading info on screen. attaching code, please guide me.
mainactivity.java:
package com.example.httpexample; import android.app.activity; import android.os.bundle; import android.widget.textview; public class mainactivity extends activity { textview httpstuff; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); httpstuff = (textview) findviewbyid(r.id.tvhttp); getmethodex test = new getmethodex(); string returned; seek { returned = test.getinternetdata(); httpstuff.settext(returned); } catch(exception e) { e.printstacktrace(); } } }
getmethodex.java:
package com.example.httpexample; import java.io.bufferedreader; import java.io.inputstreamreader; import java.net.uri; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.defaulthttpclient; public class getmethodex { public string getinternetdata() throws exception { bufferedreader in = null; string info = null; seek { httpclient client = new defaulthttpclient(); uri website = new uri("http://www.hashmedia.in"); httpget request = new httpget(); request.seturi(website); httpresponse response = client.execute(request); in = new bufferedreader(new inputstreamreader(response.getentity().getcontent())); stringbuffer sb = new stringbuffer(""); string l = ""; string nl = system.getproperty("line.separator"); while ((l = in.readline()) != null) { sb.append(l + nl); } in.close(); info = sb.tostring(); homecoming data; } { if(in!=null) { seek { in.close(); homecoming data; } catch(exception e) { e.printstacktrace(); } } } } }
activity_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.httpexample.mainactivity" > <scrollview android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:id="@+id/tvhttp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="loading data" /> </scrollview> </linearlayout>
simple, website view via intent,
intent viewintent = new intent("android.intent.action.view", uri.parse("http://www.hashmedia.in")); startactivity(viewintent);
use simple code toview website in android app.
if want particular info utilize webservice using json.
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
add net permission in manifest file,
< uses-permission android:name="android.permission.internet" />
android
Comments
Post a Comment