Pages

Sunday, May 5, 2013

Json Parsing


package com.rhmc.myserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MyServerActivity extends Activity implements OnClickListener
{
    /** Called when the activity is first created. */
    EditText username,password,e1,e2;
    Button insert,display,delete,login;
    InputStream is;
    ArrayList<NameValuePair> nameValuePairs = null;
    StringBuilder sb=null;
    String result=null;
    ArrayList<String> finaldata1 = new ArrayList<String>();
    ArrayList<String> finaldata2 = new ArrayList<String>();
    Spinner spi1,spi2;
    int j=0;
    //JSONArray jArray=null;
@Override
   
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
        e1 = (EditText)findViewById(R.id.e1);
        e2 = (EditText)findViewById(R.id.e2);

        username = (EditText)findViewById(R.id.txtunm);
        password = (EditText)findViewById(R.id.txtpwd);
        insert = (Button)findViewById(R.id.btnlogin);
        display = (Button)findViewById(R.id.btndisplay);
        spi1 = (Spinner)findViewById(R.id.spinner1);
        spi2 = (Spinner)findViewById(R.id.spinner2);
        delete= (Button)findViewById(R.id.delete);
        login= (Button)findViewById(R.id.lg);
       
        insert.setOnClickListener(this);
        display.setOnClickListener(this);
        delete.setOnClickListener(this);
        login.setOnClickListener(this);
}
public void onClick(View v) {
if(v == insert)
{
nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("title",username.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("details",password.getText().toString()));
       
        HttpClient httpclient = new DefaultHttpClient();
       
        HttpPost httppost = new HttpPost("http://10.0.2.2/aa/insert.php");
       
        try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
        catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
        try
        {
HttpResponse res = httpclient.execute(httppost);
HttpEntity entity = res.getEntity();
is = entity.getContent();
}
        catch (ClientProtocolException e)
        {
e.printStackTrace();
}
        catch (IOException e)
{
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "record successfully inserted", Toast.LENGTH_SHORT).show();
}
if(v == display)
{
try
{
finaldata1.clear();
finaldata2.clear();
HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/aa/display.php");
        HttpResponse res = httpclient.execute(httppost);
HttpEntity entity = res.getEntity();
is = entity.getContent();






BufferedReader reader = new BufferedReader(new InputStreamReader(is));
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
   
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}

String fd_username=null;
String fd_password=null;
try
{
      JSONArray jArray = new JSONArray(result);
      JSONObject json_data=null;
     
      for(int i=0;i<jArray.length();i++)
      {
json_data = jArray.getJSONObject(i);
fd_username=json_data.getString("title");
fd_password=json_data.getString("details");

finaldata1.add(fd_username.toString());
finaldata2.add(fd_password.toString());
Toast.makeText(getApplicationContext(), "hi"+finaldata1, 10).show();
      }
}
catch(JSONException e1)
{
Toast.makeText(getBaseContext(), "No RECORD Found", Toast.LENGTH_LONG).show();
}
catch (ParseException e1)
{
e1.printStackTrace();
}

spi1.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,finaldata1));

spi1.setOnItemSelectedListener(new OnItemSelectedListener()
{

public void onItemSelected(AdapterView<?> adb, View arg1,
int pos, long arg3)
{
String user = (String)adb.getItemAtPosition(pos);
username.setText(user);

}

public void onNothingSelected(AdapterView<?> arg0)
{

}
});

spi2.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,finaldata2));
spi2.setOnItemSelectedListener(new OnItemSelectedListener()
{

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String pwd = (String)arg0.getItemAtPosition(arg2);
password.setText(pwd);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}


});
}
if(v == delete)
{
try
{
nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("unm",username.getText().toString()));
       
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/anil/delete.php");
        try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
        catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
        HttpResponse res = httpclient.execute(httppost);
HttpEntity entity = res.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
}
}
if(v == login){

try
{
finaldata1.clear();
finaldata2.clear();
HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/anil/display.php");
        HttpResponse res = httpclient.execute(httppost);
HttpEntity entity = res.getEntity();
is = entity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
   
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}

String fd_username=null;
String fd_password=null;
try
{
      JSONArray jArray = new JSONArray(result);
      JSONObject json_data=null;
     
      for(int i=0;i<jArray.length();i++)
      {
json_data = jArray.getJSONObject(i);
fd_username=json_data.getString("username");
fd_password=json_data.getString("password");

finaldata1.add(fd_username.toString());
finaldata2.add(fd_password.toString());
Toast.makeText(getApplicationContext(), "hi"+finaldata1, 10).show();
      }
}
catch(JSONException e1)
{
Toast.makeText(getBaseContext(), "No RECORD Found", Toast.LENGTH_LONG).show();
}
catch (ParseException e1)
{
e1.printStackTrace();
}



}
}
}