Sunday, July 10, 2011

JSON Example.. Sending data using URLConnection Post method in Android

If we like to create a android apps there will be some necessary to send the data from android device to server. The below program will help us to send data using post method in android

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView Tname,TCountry,TDob,TCity;
EditText Ename,ECountry,EDob,ECity;
Button btnCreate;
String page="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
btnCreate = (Button) findViewById(R.id.btnGen);
Tname = (TextView) findViewById(R.id.txtName);
Ename = (EditText) findViewById(R.id.editName);
TCity = (TextView) findViewById(R.id.txtCity);
ECity = (EditText) findViewById(R.id.editCity);
TCountry = (TextView) findViewById(R.id.txtCountry);
ECountry = (EditText) findViewById(R.id.editCountry);
TDob = (TextView) findViewById(R.id.txtDob);
EDob = (EditText) findViewById(R.id.editDob);

btnCreate.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
examineJSONFile();
}
});
}

void examineJSONFile()
{
try
{
JSONObject object=new JSONObject();
object.put("Name", Ename.getText());
object.put("City", ECity.getText());
object.put("Country", ECountry.getText());
object.put("Dob", EDob.getText());
String str=object.toString();
executeHttpPost(str);
Log.i("JsonString :", str);
Toast.makeText(this, "Json Objects are : " + str,Toast.LENGTH_LONG).show();


}
catch (Exception je)
{

}
}

public void executeHttpPost(String string) throws Exception
{
//This method for HttpConnection
try
{
HttpClient client = new DefaultHttpClient();

HttpPost request = new HttpPost("http://192.168.1.118:80/androidjsontest/recieve.php");

List value=new ArrayList();

value.add(new BasicNameValuePair("Name",string));

UrlEncodedFormEntity entity=new UrlEncodedFormEntity(value);

request.setEntity(entity);

client.execute(request);

System.out.println("after sending :"+request.toString());

}
catch(Exception e) {System.out.println("Exp="+e);
}

}

}
//main.xml
//==============





















1 comment:

  1. is it possible to put a list data in json object.

    object.put("Client", Object.getClientList());

    Is this possible

    ReplyDelete