Doing HTTP POST 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_post"
      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_postActivity"
                  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_post;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.ByteArrayBuffer;

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

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

	TextView m_TextView01;
	//= (ImageButton)findViewById(R.id.ImageView01);

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

        m_TextView01=(TextView)findViewById(R.id.TextView01);
        Internet_Http_postData();
    }

    public void Internet_Http_postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.powenko.com");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse httpResponse = httpclient.execute(httppost);

            InputStream is = httpResponse.getEntity().getContent();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(20);

             int current = 0;
             while((current = bis.read()) != -1){
                    baf.append((byte)current);
             }  

            /* Convert the Bytes read to a String. */
             String text = new String(baf.toByteArray());
             m_TextView01.setText(text);

            /*
            if(httpResponse.getStatusLine().getStatusCode() == 200)
	        { 

	           String  strResult2 = EntityUtils.toString(httpResponse.getEntity());
	           m_TextView01.setText(strResult2);
	        }
	        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_getActivity"

By admin-powenko

Dr. Powen Ko is a teacher and CEO on LoopTek LLC, and like to teaching. if you need to class, please let PowenKo know, he will love to service and sharing. LoopTek web site is www.looptek.com

Leave a Reply