Wednesday, 3 August 2016

GooglePlace | GoogleMap API's Parsing | Example Using Toast

Code verified on :- Android studio 2.1.2

Step 1:- Open Android Studio.
Step 2:-Click on File > New > New Project.
Step 3:- Open activity_main.xml file and  write following code.

<?xml version="1.0" encoding="utf-8"?>
<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="androidhubb.googleplace2.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Google Place API's" />

</RelativeLayout>

Step 4:- To use HTTP client we need to add following for this follow the rule
Step 5:- Click on > Gradle Scripts >build.gradle(Module:app) >a page will open with name of app >add following 

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "androidhubb.googleplace2"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'



}

Step 6:- Open MainActivity.java file and write following code..


package androidhubb.googleplace2;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    ProgressDialog pDialoge;
    String msg="";
    @Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pDialoge=new ProgressDialog(this);
        pDialoge.setMessage("Loading");

        MyTask t=new MyTask();
        t.execute();
    }

    //clss to use webservices...   
 public class MyTask extends AsyncTask<String,String,String>{
        @Override        
protected void onPreExecute() {
            super.onPreExecute();
            pDialoge.show();

        }

        @Override        protected String doInBackground(String... strings) {
            try {

                DefaultHttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet("https://maps.googleapis.com/maps/api/place/nearbysearch/
json?location=17.436,78.4482&radius=500&types=food&key=AIzaSyA13bviUDeHP_A3WmhZaXPwC67GaUrdd3Y");
                HttpResponse res=client.execute(get);
                InputStream is=res.getEntity().getContent();
                int i=is.read();
                while(i!=-1){
                    msg=msg+(char)i;
                    i=is.read();

                }

            }
            catch (Exception e){}
            return null;
        }

        @Override        
protected void onPostExecute(String s) {
            super.onPostExecute(s);
            pDialoge.dismiss();
            Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();
        }

        @Override       
 protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            //while background task running we can handdel UI by using this method.showing downloading dialoge...        
}
    }
}

Step 7:- Open manifest.xml file and add following permission

<manifest xmlns:android="http://schemas.android.com/apk/res/android"   
 package="androidhubb.googleplace2">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

OutPut:-




No comments:

Post a Comment