layout an XML website on my Android APP

125 views Asked by At

I've been searching for a long time to answer to my simple question but haven't found it yet.

I've just started Android Development and I can't manage to layout this simple XML to the Android App I have just created.

There is my code :

  public class MainActivity extends Activity {
      private static final String TAG = null;

    /** Called when the activity is first created. */
      private String getPage() {
            String str = null ;
            Log.v(TAG, "testentreemethode");

            try
            {
                HttpClient hc = new DefaultHttpClient();
                Log.v(TAG, "testnew");
                HttpPost post = new HttpPost("http://www.3pi.tf/test.xml");
                Log.v(TAG, "testurl");
                HttpResponse rp = hc.execute(post);
                Log.v(TAG, "testpost");

                if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                {
                    str = EntityUtils.toString(rp.getEntity());
                }
            }catch(IOException e){
                e.printStackTrace();
            }  

            return str;
        }

@Override
public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  TextView txt = (TextView) findViewById(R.id.textview1);
  Log.v(TAG, "test1");
  txt.setText(getPage());
  Log.v(TAG, "test2");
 }
} 

As you can see I put some Logcat to see where the "cursor" goes and it can't pass this line:

HttpResponse rp = hc.execute(post);

Can someone help me please?

2

There are 2 answers

2
Xaver Kapeller On

Network operation cannot be performed on the main thread. Use an AsyncTask to execute it on a seperate thread like this:

public class GetXmlTask extends AsyncTask<Void, Void, String> {

    // WeakReferences are used to prevent memory leaks.
    // Always use WeakReferences when referencing Views or Activities or a Context from a seperate thread
    private final WeakReference<TextView> textViewReference;
    private final String url;

    public GetXmlTask(TextView textView, String url) {
        this.textViewReference = new WeakReference<TextView>(textView);
        this.url = url;
    }

    @Override
    protected String doInBackground(Void... params) {
        HttpClient hc = new DefaultHttpClient();
        Log.v(TAG, "testnew");
        HttpPost post = new HttpPost(url);
        Log.v(TAG, "testurl");
        HttpResponse rp = hc.execute(post);
        Log.v(TAG, "testpost");

        if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
        {
            return EntityUtils.toString(rp.getEntity());
        }
        return "Error";
    }   

    @Override
    protected void onPostExecute(String result) {       
        TextView textView = textViewReference.get();
        if(textView != null) {
            textView.setText(result);
        }       
    }
}

You can execute the task like this:

GetXmlTask task = new GetXmlTask(textView, "http://www.3pi.tf/test.xml");
task.execute(); 
4
Mr.Me On

In any application you should avoid IO calls on main thread because it is used to handle user events and UI in general. in android doing so causes NetworkOnMainThreadException

Try to move your web calls to a background thread and it should work.

ex

public class MainActivity extends Activity {
TextView textView;
Handler mHandler;
  private static final String TAG = null;

/** Called when the activity is first created. */
  private String getPage() {
        String str = null ;
        Log.v(TAG, "testentreemethode");

        try
        {
            HttpClient hc = new DefaultHttpClient();
            Log.v(TAG, "testnew");
            HttpPost post = new HttpPost("http://www.3pi.tf/test.xml");
            Log.v(TAG, "testurl");
            HttpResponse rp = hc.execute(post);
            Log.v(TAG, "testpost");

            if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                str = EntityUtils.toString(rp.getEntity());
            }
        }catch(IOException e){
            e.printStackTrace();
        }  

        return str;
    }

 @Override
public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 txtView = (TextView) findViewById(R.id.textview1);
 mHandler = new Handler();
 new Thread(){
    @Override
  public void run(){
    final String str = getPage();
    mHandler.post(new Runnable(){
         @Override
      public void run(){
         textView.setText(str);
       }
   });
 }
  }.start();
Log.v(TAG, "test1");
Log.v(TAG, "test2");
}
} 

Please take a look at this tutorial for better understanding of android threadining. tutorial