0

I am new to android development. I am simply trying to get some data from internet and want to display it. It is showing a blank page when I run the program, no error shows up.

Thanks

HttpExample.java

package com.example.bucky1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HttpExample extends Activity {

    TextView httpStuff;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httpexample);
        httpStuff =(TextView)findViewById(R.id.tvhttp);
      GetMethodEx getdata = new GetMethodEx();
      String returned;
      try
      {
          returned = getdata.getInternetData();
          httpStuff.setText(returned);
      }
      catch(Exception e){
          e.printStackTrace();
      }
    }



}

GetMethod.java

package com.example.bucky1;

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 data = null;

try{
    HttpClient Client = new DefaultHttpClient();
    URI website = new URI("http://www.mybringback.com");
    HttpGet request = new HttpGet();
    request.setURI(website);
    HttpResponse response = Client.execute(request);
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
   String l ="";
   String nl = System.getProperty("line.seprator");
   StringBuffer sb = new StringBuffer("");
   while((l = in.readLine()) != null )
   {
       sb.append(l + nl);

   } in.close();
    data = sb.toString();
    return data;
}

 finally{
     if(in != null)
     { 
         try{
         in.close();
         return data;
       }
         catch(Exception e) {
             e.printStackTrace();
         }

          }
     }





}
}

httpexample.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<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="TextView" />
</ScrollView>
</LinearLayout>
pt2121
  • 11,720
  • 8
  • 52
  • 69

2 Answers2

3

There are multiple things you need to do in order to make things work

  1. Make sure you have the internet permission set. Refer to What permission do I need to access Internet from an android application?
  2. You can only connect to the internet on a separate thread that isn't the UI thread. Refer to http://developer.android.com/reference/android/os/AsyncTask.html (You only really need doInBackground and onPostExecute)
  3. You can only edit the UI on the UI thread, so use the onPostExecute method in your AsyncTask
  4. An easier way to get the content of you response is to use EntityUtils.toString(response.getEntity)
Community
  • 1
  • 1
Anubhaw Arya
  • 145
  • 6
  • No problem :D. Do us all a favor though and mark my answer as an accepted answer so that others know it's been answered. – Anubhaw Arya Jun 27 '13 at 17:28
0
package com.example.bucky1;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;


public class HttpExample extends Activity{

    TextView httpStuff;

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // ADD 2 LINES
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        // -------------
        super.onCreate(savedInstanceState);
        setContentView(R.layout.httpex);
        httpStuff = (TextView) findViewById(R.id.tvHttp);
        GetMethodEx test = new GetMethodEx();       
        String returned;
        try {
            returned = test.getInternetData();
            httpStuff.setText(returned);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}