Monday, 31 August 2015

Android Programming Tutorial 42 : WebServices Using GET Method In Android

Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: WebServicesUsingGETApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

    import java.io.IOException;
    import java.io.InputStream;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.protocol.BasicHttpContext;
    import org.apache.http.protocol.HttpContext;

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

    public class MainActivity extends Activity implements OnClickListener {

                Button my_button;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                findViewById(R.id.btn_REST).setOnClickListener(this);
                 }
                @Override
                public void onClick(View v) {
                my_button = (Button) findViewById(R.id.btn_REST);
                my_button.setClickable(false);
                new GetDataRESTfull().execute();
                }
    public class GetDataRESTfull extends AsyncTask<Void, Void, String> {
                protected String getASCIIContentFromEntity(HttpEntity entity)
                throws IllegalStateException, IOException {
                InputStream inputStream = entity.getContent();
                StringBuffer stringBuffer = new StringBuffer();
                int n = 1;
                while (n > 0) {
                byte[] b = new byte[4096];
                n = inputStream.read(b);
                if (n > 0)
                stringBuffer.append(new String(b, 0, n));
                }// while
                return stringBuffer.toString();
                }// getASCII
                @Override
                protected String doInBackground(Void... params) {
                HttpClient httpClient = new DefaultHttpClient();
                HttpContext httpContext = new BasicHttpContext();
                //Here we are Using GET Method 
                 
                HttpGet httpGet = new HttpGet(
     "http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=1");
                String text = null;
                try {
    HttpResponse httpResponse = httpClient.execute(httpGet,
                httpContext);
                HttpEntity httpEntity = httpResponse.getEntity();
                text = getASCIIContentFromEntity(httpEntity);
               } catch (Exception e) {
               e.getLocalizedMessage();
               }
               return text;
              }// doInBack
              @Override
              protected void onPostExecute(String result) {
              if (result != null) {
      EditText editTextREST = (EditText) findViewById(R.id.editTextREST);
              editTextREST.setText(result);
              }// if
              my_button.setClickable(true);
                            }// doPost
                }
    }



     2.Simple Copy and Past the below code:  actvitiy_main.xml

    <LinearLayout 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:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="RESTful WebServices"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#176CEC"
            android:gravity="center" />

        <Button
            android:id="@+id/btn_REST"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GET Data"
            android:layout_marginLeft="100dp"
            android:padding="14dp" />

        <EditText
            android:id="@+id/editTextREST"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:minLines="13"
            android:editable="false"
            android:textSize="12sp"
            android:maxLines="13"
            android:ems="10" />

    </LinearLayout>

    3.Copy and past the code for manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ambilpursunil.newapp"
        android:versionCode="1"
        android:versionName="1.0" >

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

        <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>

    4.Right click on the project and Run As-->Android Application


    OutPut:

    When our application launch you will find a GET Button and a EditText simply click on the GET Button and see the output on the screen.

    Please Send Your Comments To ambilpura.sunil@gmail.com




    Stay Tune For Next Tutorial...  ActionBar In Android:

    No comments:

    Post a Comment