1. Check out our companion app, Forums for Android! Download from Google Play

Doing HTTP Request with Android

Discussion in 'Application Development' started by et1carmen, Sep 13, 2009.

  1. et1carmen

    et1carmen New Member
    Thread Starter
    5

    Sep 13, 2009
    1
    0
    5
    HI, as I am a beginner for Android.
    Now I am trying to do a test application for sending HTTP Request.
    But I find that my coding is not work.
    So I hope someone can give me some ideas on it.
    The following is my code:

    package com.example.helloandroid;
    import java.io.IOException;
    import android.app.Activity;
    import android.os.Bundle;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.*;
    import org.apache.http.client.methods.*;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;

    public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    try
    {
    HttpClient hc = new DefaultHttpClient();
    HttpPost post = new HttpPost("www.yahoo.com.hk");

    HttpResponse rp = hc.execute(post);

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

    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(str);
    //setContentView(R.layout.main);
    }
    }
     

    Advertisement

  2. Visarex

    Visarex New Member
    6

    Oct 7, 2010
    2
    1
    6
    Hy et1carmen.

    The code you put up is actually very good for making http requests and retrieving the response but you did not place it in the right place.

    Also, "setContentView(str);" is not correct.
    You cannot set the view to a string, you need a container to display the string.
    Here's how you do it:

    -go to main.xml and add a textView container :
    Code (Text):
    1. <TextView android:layout_height="wrap_content"
    2.     android:id="@+id/textView1"
    3.     android:text=""
    4.     android:layout_width="wrap_content"></TextView>
    You will have one if you checked "Create activity" when making the project, just add an android:id like the one above.

    -open HelloAndroid.java and move your code request related code in a function like this:
    Code (Text):
    1. private String getPage() {
    2.         String str = "***";
    3.  
    4.         try
    5.         {
    6.             HttpClient hc = new DefaultHttpClient();
    7.             HttpPost post = new HttpPost("http://www.yahoo.com");
    8.  
    9.             HttpResponse rp = hc.execute(post);
    10.  
    11.             if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
    12.             {
    13.                 str = EntityUtils.toString(rp.getEntity());
    14.             }
    15.         }catch(IOException e){
    16.             e.printStackTrace();
    17.         }  
    18.        
    19.         return str;
    20.     }
    This return the entire content of the webpage Yahoo!.
    CAUTION:DON'T FORGET TO ADD "HTTP://"!
    If you don't you will get an exception.

    -AFTER "setContentView(R.layout.main);" add the following code:
    Code (Text):
    1. TextView txt = (TextView) findViewById(R.id.textView1);
    2.           txt.setText(getPage());
    This will return the string content of the page.

    -add Internet access permision by editing AndroidManifest.xml (add this BEFORE </manifest>):
    Code (Text):
    1. <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    Note that this will display the source code of the webpage.
    If you want to see the webpage itself see WebView | Android Developers.

    Hope this helps.
    Cheers.
     
    Arvesh likes this.
  3. rlshaw

    rlshaw New Member
    5

    Feb 11, 2011
    2
    0
    5
    for some reason the debugger wont work this application can you help me figure out why this is happen
     
  4. StreetDogX

    StreetDogX New Member
    5

    Mar 16, 2011
    1
    0
    5
    You need this import

    Code (Text):
    1. import android.widget.TextView;
     

Share This Page

Loading...