0

I'm very new to android development. I'm simply trying to grab text from a website (let's say it's called http://foo.com/meg_message) and use this text to fill a TextView. What is the best way to do this?

I have the following XML element:

 <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="0dp">
        <TextView
            android:id="@+id/meg_message"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:textColor="@color/white"
            android:textSize="50dp"
            tools:text=""/>
    </LinearLayout>
Snubber
  • 986
  • 1
  • 10
  • 24

2 Answers2

2

If your http://foo.com/meg_message returns a response when you call it ,

In manifest use internet permissions

<uses-permission android:name="android.permission.INTERNET" />

Get you response like this

 DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(yourURL);
            HttpResponse response = httpclient.execute(httpget);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {                    
                sb.append(line + NL);
            }
            in.close();
            String result = sb.toString();
            Log.v("Response : ", result);

yourURL example :http://foo.com/meg_message

Now ready a textView

TextView tv = (TextView)findViewById(R.id.textView_id);
tv.setText(result);
Charuක
  • 12,953
  • 5
  • 50
  • 88
1

If it is your own website you can call javascript method to invoke your java native methord and fill whatever required. Please refer this answer for more info

Call Android methods from JavaScript

Community
  • 1
  • 1
Askarc Ali
  • 318
  • 4
  • 21