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();
}



}
}
}

Wednesday, March 20, 2013

Button

Project Name : Button
Application Name : Button
Package Name : com.demo.button
Activity Name : ButtonActivity


Main.xml


<?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"
    >
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

ButtonActivity.java


package com.demo.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ButtonActivity extends Activity implements OnClickListener{

Button b;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        b = (Button)findViewById(R.id.button1);
        b.setOnClickListener(this);
    }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), "Button pressed..", 10).show();

}
}



Friday, March 15, 2013

Android Installation

1. Download and Install Java(jdk)  Download
2. Download and Open Eclipse  Download
3. Download and Install Android sdk Download
  A pop-up will come out just after you finished installing the SDK, which is the SDK manager. If no pop-up      window comes then you can manually open it from ‘All Program’

4.Download and Install ADT plugin for Eclipse
   Start Eclipse > Help > Install New Software....
   then click on Add top Right
   when Dialog appears enter "ADT" for the NAME  and following url for LOCATION

URL : https://dl-ssl.google.com/android/eclipse/

then OK

5. Config Android sdk

  select Window> Preferences..
  select Android from Left side ..
  after you may see dialog asking whether you wants to send usage statistics to Google..
  then click on Proceed
  for sdk location  click on Browse and locate your downloaded SDK directory...
 click Apply then OK
Get it on Google Play