URL:
http://cancos.quadlogix.com/auth/friends
MainActivity.java
---------------------
package com.kundan.jsonparsingex;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String URL="http://cancos.quadlogix.com/auth/friends";
Button btn,btn2;
static ListView lv;
static TextView tv1;
static ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView1);
tv1=(TextView) findViewById(R.id.textView1);
iv=(ImageView) findViewById(R.id.imageView1);
btn=(Button) findViewById(R.id.button1);
btn2=(Button) findViewById(R.id.button2);
btn2.setEnabled(false);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GetJSONData task = new GetJSONData();
task.execute(new String[] { URL });
Toast.makeText(getApplicationContext(), "please wait till image and text loaded...", Toast.LENGTH_LONG).show();
btn2.setEnabled(true);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
lv.setAdapter(new MyImageAdapter(getApplicationContext(), GetJSONData.mIcon,GetJSONData.nameID));
}
});
}
}
GetJSONData.java
---------------------------
package com.kundan.jsonparsingex;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
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.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;
public class GetJSONData extends AsyncTask<String, Void, String> {
static String[] nameID=null;
static Bitmap[] mIcon=null;
static String[] imageID=null;
@Override
protected String doInBackground(String... urls) {
String output = null;
for (String url : urls) {
output = getOutputFromUrl(url);
}
return output;
}
private String getOutputFromUrl(String url) {
String output = null;
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
output = EntityUtils.toString(httpEntity);
System.out.println(output);
try {
JSONObject jobj=new JSONObject(output);
JSONArray jarray=jobj.getJSONArray("data");
System.out.println("////////"+jarray);
JSONObject jobject=null;
nameID=new String[jarray.length()];
imageID=new String[jarray.length()];
mIcon = new Bitmap[jarray.length()];
for(int i=0;i<jarray.length();i++){
jobject=jarray.getJSONObject(i);
nameID[i]=jobject.getString("name");
imageID[i]=jobject.getString("profile_image");
mIcon[i] = StringToBitMap(imageID[i]);
//System.out.println("++++++++++"+nameID[i]);
//System.out.println("++++++++++"+imageID[i]);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
@Override
protected void onPostExecute(String output) {
super.onPostExecute(output);
MainActivity.tv1.setText(nameID[0]);
MainActivity.iv.setImageBitmap(mIcon[0]);
}
public Bitmap StringToBitMap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,encodeByte.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}
}
MyImageAdapter.java
---------------------------
package com.kundan.jsonparsingex;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyImageAdapter extends BaseAdapter {
Context context;
String[] nameID;
Bitmap[] imgId;
static String str1;
public MyImageAdapter(Context context,Bitmap[] ImageID, String[] nameID ) {
this.context=context;
this.imgId=ImageID;
this.nameID=nameID;
}
@Override
public int getCount() {
return nameID.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.image_view, parent,false);
TextView name=(TextView) rowView.findViewById(R.id.nameView);
ImageView image=(ImageView) rowView.findViewById(R.id.imageView1);
name.setText(nameID[position]);
image.setImageBitmap(imgId[position]);
return rowView;
}
}
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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#990000"
android:dividerHeight="1dip"
android:paddingTop="5dip"
android:layout_marginTop="150dp">
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Profile" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginTop="16dp"
android:text="View all Profile" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginLeft="19dp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
</RelativeLayout>
image_view.xml
---------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/imageView1"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000099" />
</RelativeLayout>
No comments:
Post a Comment