• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Doing HTTP Request with Android

et1carmen

Lurker
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);
}
}
 
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:
<TextView android:layout_height="wrap_content" 
	android:id="@+id/textView1" 
	android:text="" 
	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:
private String getPage() {
    	String str = "***";

        try
    	{
    		HttpClient hc = new DefaultHttpClient();
    		HttpPost post = new HttpPost("http://www.yahoo.com");

    		HttpResponse rp = hc.execute(post);

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

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:
TextView txt = (TextView) findViewById(R.id.textView1);
          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:
<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.
 
Back
Top Bottom