Find us on facebook

Showing posts with label Android/Eclipse. Show all posts
Showing posts with label Android/Eclipse. Show all posts

May 27, 2015

Simple Login Form - Using web services - Android/PHP




Step 01 : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firstapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 02 : activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:hint="@string/Email"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="67dp"
        android:ems="10"
        android:hint="@string/Password"
        android:inputType="textPassword" />

      <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="22dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/progressBar1"
        android:layout_marginLeft="22dp"
        android:text="@string/Login" />

</RelativeLayout>

Step 03 : MainActivity.java

package com.example.firstapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
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.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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


public class MainActivity extends Activity {
    EditText password,email;
    Button login;
    ProgressBar progressBar;
   
   
       
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        password=(EditText) findViewById(R.id.editText2);
        email=(EditText) findViewById(R.id.editText1);
        login=(Button) findViewById(R.id.button1);
               
        progressBar=(ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.GONE);
       
        login.setOnClickListener(new OnClickListener() {
       
            public void onClick(View v) {
                progressBar.setVisibility(View.VISIBLE);
                new ExecuteTask().execute(email.getText().toString(),password.getText().toString());
               
            }
        });
       

    }
   
     class ExecuteTask extends AsyncTask<String, Integer, String>
        {

            @Override
            protected String doInBackground(String... params) {
               
                String res=PostData(params);
               
                return res;
            }
           
            @Override
            protected void onPostExecute(String result) {
            progressBar.setVisibility(View.GONE);
           }
           
        }
   
    public String PostData(String[] valuse) {
        String s="";
        try
        {
        HttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost("http://batransfer.cf/mobile/member-login");
       
        JSONObject loginDetails = new JSONObject();
       try {
        loginDetails.put("email", valuse[0]);
        loginDetails.put("password", valuse[1]);
         

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        StringEntity se = new StringEntity(loginDetails.toString());
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpPost.setEntity(se);
        HttpResponse httpResponse=  httpClient.execute(httpPost);
   
        //HttpEntity httpEntity=httpResponse.getEntity();
        result= readResponse(httpResponse);
        }
        catch(Exception exception)  {}
        return result;
   
       
    }
    public String readResponse(HttpResponse res) {
        InputStream is=null;
        String return_text="";
        try {
            is=res.getEntity().getContent();
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
            String line="";
            StringBuffer sb=new StringBuffer();
            while ((line=bufferedReader.readLine())!=null)
            {
            sb.append(line);
            }
            return_text=sb.toString();
        } catch (Exception e)
        {
           
        }
        return return_text;
       
    }
   

}  

JSONObject Vs JSONArray

We can use JSONArray to parse JSON which starts with the array brackets. A collection of related items can be included in JSONArray. (Number of JSON objects can be included within JSONArray).
EG:
 [
       {"label":"HA0-Brent","id":"1085","table":"plot_dis"},
       {"label":"HA1-Harrow","id":"1086","table":"plot_dis"},
       {"label":"HA2-Harrow","id":"1087","table":"plot_dis"},
       {"label":"HA3-Harrow","id":"1088","table":"plot_dis"},
       {"label":"HA4-Hillingdon","id":"1089","table":"plot_dis"}
  ]

We can  use JSONObject when dealing with JSON that begins with curly braces. A JSON object contains key/value pairs related to item.
EG: {"email":"testbawebservice@gmail.com","password":"testba123"}

JSON arrays and objects can be nested in one another.
EG:{
"code":"0",
"message":"success",
 "locations":[
       {"label":"HA0-Brent","id":"1085","table":"plot_dis"},
       {"label":"HA1-Harrow","id":"1086","table":"plot_dis"},
       {"label":"HA2-Harrow","id":"1087","table":"plot_dis"},
       {"label":"HA3-Harrow","id":"1088","table":"plot_dis"},
       {"label":"HA4-Hillingdon","id":"1089","table":"plot_dis"}
  ]}

Using web services with Android App - Provide Internet permission

First,to use web services with android,

We need to provide INTERNET permission in AndroidManifest.xml file.

 <uses-permission android:name="android.permission.INTERNET" />


Android R.java File

aapt (Android Asset Packaging Tool) auto-generates R.java file. This file contains resource IDs for all the resources of res/ directory.

activity_main.xml file contains many components. For these each specific component, id is automatically created in this file. This id can be used in the activity source file to perform any action on the component.

If R.jar file is deleted, automatically it will be created by android.

strings.xml

In Eclipse the strings.xml file is used to declare strings instead of hard-coding them into other project files.

Place your strings in strings.xml file like shown in this example, and reference those strings in your other project files.

FirstApp/res/layout/activity_main.xml

 <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="67dp"
        android:ems="10"
        android:hint="@string/Password"
        android:inputType="textPassword" />

FirstApp/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="Password">Password</string>
</resources>

Failed to install FirstApp.apk on device emulator-5554

When this error appears with the red font, don't close anything - leave it there and then press the run button again. 

May 26, 2015

Fix Error Code 13 For Eclipse



Open eclipse.ini file

-vm
C:/Program Files/Java/jre6/bin/javaw.exe (Path to javaw.exe file)

Put above part before -vmargs