Friday, January 15, 2010

Communication with HttpRequest


Get data from server with help of http request and get the response.
Java file:

package com.example.HttpExample;
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.protocol.HTTP;
import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HttpExample extends Activity {
    /** Called when the activity is first created. */
     String url = "http://techy.limewebs.com/data.php";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
  
 
   TextView tv=new TextView(this);
   tv.setText("Android !!!!!!!!!!!!");
   Button myButton = (Button) findViewById(R.id.button);
   myButton.setOnClickListener(new OnClickListener() {
   
    public void onClick(View v) {
        // TODO Auto-generated method stub
        btn_Click();
    }}
);
  // setContentView(tv);
  
    }
    public void btn_Click(){
        //alertbox("My Alert","U click Me");
        SendHttpRequset();
       
       
    }
    protected void alertbox(String title, String mymessage) 
        { 
        new AlertDialog.Builder(this) 
           .setMessage(mymessage) 
          .setTitle(title) 
           .setCancelable(true) 
           .setNeutralButton(android.R.string.cancel, 
              new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog, int whichButton){} 
              }) 
           .show(); 
        }

    private void SendHttpRequset(){
   
         try {
               HttpClient httpclient = new DefaultHttpClient(); 
               HttpPost httppost = new HttpPost(url); 
              
              
               List nameValuePairs = new ArrayList(); 
               nameValuePairs.add(new BasicNameValuePair("data", "Atul Yadav"));   
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8)); 

               // Execute HTTP Post Request 
               HttpResponse response = httpclient.execute(httppost);
              
               InputStream is = response.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. */
                TextView lbl = (TextView) findViewById(R.id.ShowResponce);
                String  text = new String(baf.toByteArray());
                lbl.setText(text);
             

            } catch (ClientProtocolException e) { 
                // TODO Auto-generated catch block
                 TextView lbl = (TextView) findViewById(R.id.ShowResponce);
                 lbl.setText(e.getMessage());
               
           } catch (IOException e) { 
                // TODO Auto-generated catch block
                 TextView lbl = (TextView) findViewById(R.id.ShowResponce);
                 lbl.setText(e.getMessage());
             
           } 
            }
   
}



Layout :


Main.xml


    
    

1 comment:

  1. Thanks for posting this.. it's pretty much exactly what I've spent all day looking for. However I can't get it to work! I'm very new to this so it's probably something I'm doing but I get this error in the main.xml:

    error: No resource identifier found for attribute 'onclick' in package 'android'

    I grabbed the main.xml from the source code for this page as it seems to have rendered it!

    I tried just removing the onclick section in main.xml and it force closed when I ran it.

    Thanks in advance for your advice!

    ReplyDelete