Monday, 24 October 2016

JSON in Android

Hi All,
Using JSON we can send as many items to another resource.
See the code below, where the data from the interface is used to create a JSON object and the same is pushed to a webresource 'booking.php' for saving.



// Code

Hi Just see one function where you can find how to add an event handler

package com.finder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class Bookings  extends Activity{
EditText ename,ecrd1,etime;
TextView tname,tcrd,ttime;
Button ok,view;
public static String name;
public static String crd;
public static String time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.booking);
        ename=(EditText)findViewById(R.id.etname);
        ecrd1=(EditText)findViewById(R.id.etcrd);
        etime=(EditText)findViewById(R.id.ettime);
        tname=(TextView)findViewById(R.id.tvname);
        tcrd=(TextView)findViewById(R.id.tvcrd);
        tname=(TextView)findViewById(R.id.tvname);
        ttime=(TextView)findViewById(R.id.tvtime);
        ok=(Button)findViewById(R.id.btok);
        view=(Button)findViewById(R.id.btview);
        ok.setOnClickListener(new OnClickListener() {
                        @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                   name=ename.getText().toString();
                   crd=ecrd.getText().toString();
                   time=etime.getText().toString();
                  HttpClient hclient=new DefaultHttpClient();
                    JSONObject json=new JSONObject();
                   
                    HttpPost hp=new HttpPost("http://10.0.2.2/booking.php");
            try {
                       
                        json.put("name",  name);
                        json.put("crd", crd);
                        json.put("time", time);
                       

                    } catch (JSONException e2) {

                        e2.printStackTrace();
                    }
                              
                    JSONArray postjson = new JSONArray();
                    postjson.put(json);
                   
                    hp.setHeader("json", json.toString());
                    HttpResponse response = null;
                    try {
                        response = hclient.execute(hp);
                    } catch (ClientProtocolException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                      if (response != null) {
                        InputStream is = null;
                        try {
                            is = response.getEntity().getContent();
                        } catch (IllegalStateException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
       
               BufferedReader reader = new BufferedReader(
                                new InputStreamReader(is));
                        StringBuilder sb = new StringBuilder();

                        String line = null;
                        try {
                            while ((line = reader.readLine()) != null) {
                                sb.append(line + "\n");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                is.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        //String text = sb.toString();
                    }               
           
                    //t.setText(text);
            System.out.println("555");
            Toast.makeText(getBaseContext(),"Booked",Toast.LENGTH_LONG).show();
                    }
               
           
               


        });
    
       
    }
   

}
//---------------------------------
Booking.php
        
<?php


$json = $_SERVER['HTTP_JSON'];

$data = json_decode($json);
$name = $data->name;
$crd = $data->crd;
$time = $data->time;

echo "name: ".$name;
echo "crd : ".$crd;
echo "time : ".$time;

//Write DB MYSQL code to insert into your table

//Refer phpissimple.blogspot.in for DB Code

?>

No comments:

Post a Comment