Read This Offline With Code With Me APP

Simple EarthQuake APP in Android Using JSOUP

Posted On: Jun 23, 2015

Today, I am going to show how you can create simple android APP which can show last earthquake in Nepal. The APP uses JSOUP library to fetch data from http://www.seismonepal.gov.np and shows last earthquake value to a TextView.


Main Logic:
1) Load Website data of seismonepal.gov.np
2) Parse data using JSOUP library and show to a TextView seismonepal screen

Step 1: Create an empty project in Android Studio.


Name: LASTQUAKE
Package Name: np.com.sagardevkota.jsoup;

Step 2: Download jsoup-1.8.2.jar and copy to /app/libs folder of your project


Step 3: Edit AndroidManifest.xml and add following permission above
<application></aplication>
tag


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



Step 4: Your src/res/layout/activity_main.xml should look like this


<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="4.5"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/txtData"
        android:layout_above="@+id/btnClick"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="36dp"
        android:background="@drawable/circular_shape"
        android:textSize="50dp"
        android:gravity="center"
        android:visibility="gone"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LOAD LAST QUAKE"
        android:id="@+id/btnClick"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Last Quake Is"
        android:id="@+id/textView"
        android:layout_marginBottom="27dp"
        android:layout_above="@+id/txtData"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Step 5: Create inside src/res/drawable/circular_shape.xml with following code


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#FF0000" />

    <stroke
        android:width="2dp"
        android:color="#98AFC7" />

    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp" />

</shape>

Step 6: Edit MainActivity.java with following code


package np.com.sagardevkota.jsoup;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import org.jsoup.nodes.Element;


public class MainActivity extends ActionBarActivity {
    TextView txt;
    private ProgressDialog progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progress=new ProgressDialog(this);
        progress.setCancelable(false);
        progress.setMessage("Loading data, please wait..");

        Button click=(Button) findViewById(R.id.btnClick);
        txt=(TextView) findViewById(R.id.txtData);
        //now load data initially
        sendRequest();

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                sendRequest();

            }
        });

    }

    /*
    Method to send request to seismonepal.gov.np website and
     */

    void sendRequest(){
        progress.show();
        (new AsyncTask<String,Void,String>(){
            String url;
            @Override
            protected String doInBackground(String... params) {
                url = params[0];
                Document doc = null;
                String last_quake="";
                try {
                    doc = Jsoup.connect("http://seismonepal.gov.np/").get();
                    Elements newsHeadlines = doc.select(".block2-content:eq(1) table tbody tr:eq(1) td:eq(4) span");
                    if(newsHeadlines!=null) {
                        for (Element data : newsHeadlines) {
                            Log.d("sagar", "data is " + data.html());
                            last_quake = data.html();
                            break;
                        }
                    }
                    else
                    {
                        last_quake= "ER";
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                    last_quake= "ER";
                }

                return last_quake;


            }

            @Override
            protected void onPostExecute(String s) {
                txt.setText(s);
                progress.dismiss();
                txt.setVisibility(View.VISIBLE);

            }
        }).execute("http://seismonepal.gov.np/");
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


Tags:

  • android
  • jsoup

Share This On

By Devkota Sagar On Oct 12, 2015

Thanks Daju. Daju blogger or wordpress li ni apk banauna mildina?

Reply

Comments