Tutorial 050, internet, http Get request

August 17, 2011   ·   0 Comments

Doing HTTP GET with the current SDK


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.powenko.Tutorial_internet_get"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Tutorial_internet_getActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:id="@+id/TextView01"
    />
</LinearLayout>


package com.powenko.Tutorial_internet_get;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

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

public class Tutorial_internet_getActivity extends Activity {
    /** Called when the activity is first created. */

	TextView m_TextView01;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        m_TextView01=(TextView)findViewById(R.id.TextView01);
        Internet_Http_getData();
    }
    

    public void Internet_Http_getData() {
        String uriAPI ="http://www.powenko.com"; 	   
        HttpGet httpRequest = new HttpGet(uriAPI); 
	    

	         try 
	         {         
	           HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);       
	           if(httpResponse.getStatusLine().getStatusCode() == 200)  
	           { 
	             
	           String  strResult2 = EntityUtils.toString(httpResponse.getEntity());
	    
	           
	           m_TextView01.setText(strResult2);
	            // mTextView1.setText("1111111"); 
	           } 
	           else 
	           { 
	        	   m_TextView01.setText("Error Response: "+httpResponse.getStatusLine().toString()); 
	           } 
	         } 
	         catch (ClientProtocolException e) 
	         {  
	        	 m_TextView01.setText(e.getMessage().toString()); 
	           e.printStackTrace(); 
	         } 
	         catch (IOException e) 
	         {  
	        	 m_TextView01.setText(e.getMessage().toString()); 
	           e.printStackTrace(); 
	         } 
	         catch (Exception e) 
	         {  
	        	 m_TextView01.setText(e.getMessage().toString()); 
	           e.printStackTrace();  
	         }  
		
    }
}

sample code:

"Tutorial_internet_get"
shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon