Sunday, 12 August 2012

get json response from url


public static JSONObject executeHttpGet(String uri) throws Exception{
        Log.d("test","Loading Uri:"+uri);
    HttpGet req = new HttpGet(uri);
    HttpClient client = new DefaultHttpClient();
    HttpResponse resLogin = client.execute(req);
    BufferedReader r = new BufferedReader(new InputStreamReader(resLogin.getEntity().getContent()));
    StringBuilder sb = new StringBuilder();
    String s = null;
    while ((s = r.readLine()) != null) {
    sb.append(s);
    }
       Log.d("test", "Response"+sb.toString());
    return new JSONObject(sb.toString());
    }

Creating circular PageViewer

*Java code.


package view.pager.demo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {

private int previousState, currentState;
private ViewPager viewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
viewPager = (ViewPager) findViewById(R.id.ViewPagerMainActivity);
viewPager.setAdapter(new CustomPageAdapter());
viewPager.setOnPageChangeListener(onPageChangeListener);
viewPager.setPageMargin(10);
}

OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {

@Override
public void onPageSelected(int pageSelected) {
Log.e("onPageSelected", "pageSelected:" + pageSelected);
}

@Override
public void onPageScrolled(int pageSelected, float positionOffset,
int positionOffsetPixel) {
Log.e("onPageScrolled", "pageSelected" + pageSelected
+ ",positionOffset:" + positionOffset
+ ",positionOffsetPixel:" + positionOffsetPixel);
}

@Override
public void onPageScrollStateChanged(int state) {
//Comment this code if doesn't need circular pageviewer
Log.e("onPageScrollStateChanged", "state:" + state);
int currentPage = viewPager.getCurrentItem();
if (currentPage == 3 || currentPage == 0) {
previousState = currentState;
currentState = state;
if (previousState == 1 && currentState == 0) {
viewPager.setCurrentItem(currentPage == 0 ? 3 : 0);
}
}
}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

private class CustomPageAdapter extends PagerAdapter {

@Override
public int getCount() {
return 4;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.d("test", "position:"+position);
LayoutInflater layoutInflater = (LayoutInflater) container
.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View view = null;
if (position <= container.getChildCount() - 1)
view = container.getChildAt(position);
if (view != null)
return view;
switch (position) {
case 0:
view = layoutInflater.inflate(R.layout.view1, null);
break;
case 1:
view = layoutInflater.inflate(R.layout.view2, null);
break;
case 2:
view = layoutInflater.inflate(R.layout.view3, null);
break;
case 3:
view = layoutInflater.inflate(R.layout.view4, null);
break;
default:
break;
}
((ViewPager) container).addView(view, position);
return view;
}

@Override
public void destroyItem(View view, int position, Object object) {
((ViewPager) view).removeView((View) object);

}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);

}

@Override
public Parcelable saveState() {
return null;
}
}
}

*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.support.v4.view.ViewPager
        android:id="@+id/ViewPagerMainActivity"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>


get distance between two geo points


 private static int EARTH_RADIUS_KM = 6371;

    public static int MILLION = 1000000;

    /**
     * Computes the distance in kilometers between two points on Earth.
     *
     * @param lat1 Latitude of the first point
     * @param lon1 Longitude of the first point
     * @param lat2 Latitude of the second point
     * @param lon2 Longitude of the second point
     * @return Distance between the two points in kilometers.
     */
    public static double distanceKm(double lat1, double lon1, double lat2, double lon2) {
        double lat1Rad = Math.toRadians(lat1);
        double lat2Rad = Math.toRadians(lat2);
        double deltaLonRad = Math.toRadians(lon2 - lon1);
     
        return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad)
                * Math.cos(deltaLonRad))
                * EARTH_RADIUS_KM;
    }