Json_parsing_android_hive_url
Step 1:Manifest ma internet ni permission
Step 2:build graddle(module:app)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.squareup.picasso:picasso:2.71828'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
}
Step 3:Activiry_main_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.jay.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"></ListView>
</LinearLayout>
Step 4:simple_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:orientation="vertical"
android:layout_height="100dp">
<ImageView
android:id="@+id/iv"
android:background="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Step 5:Hero //hero java ma file bnavi hero itle pojjo jema value gete and set tay
public class Hero {
private String id;
private String name;
private String email;
private String address;
private String gender;
private String mobile;
private String home;
private String office;
//aatlu lkheya pache right click kari ne getter and setter karvu
Step 6:jsonAdapter //jsonadapter name ni java ma file bnaave
public class jsonAdapter extends BaseAdapter {
Context context;
ArrayList<Hero> arryList=new ArrayList<>();
public jsonAdapter(Context context, ArrayList<Hero> arryList) {
this.context = context;
this.arryList = arryList;
}
@Override
public int getCount() {
return arryList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.simple,null);
ImageView imageView;
TextView textView;
imageView=v.findViewById(R.id.iv);
textView=v.findViewById(R.id.tv);
Hero h=arryList.get(i);
textView.setText(h.getId() +"\n"+ h.getName() +"\n"+h.getEmail() +"\n"+ h.getAddress() +"\n"+h.getGender() +"\n"+h.getMobile() +"\n"+h.getHome() +"\n"+h.getOffice());
//Picasso.get().load(h.getImageurl()).into(imageView);
return v;
}
}
Step 7:json class
public class jsonclass {
String result;
public String processurl(String s)
{
try {
URL url=new URL(s);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code=conn.getResponseCode();
if (code==200)
{
InputStream in=new BufferedInputStream(conn.getInputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line="";
while ((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
result=sb.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
Step 8:MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView lv;
String URL_GATE_DATA="https://api.androidhive.info/contacts/",result;
ArrayList<Hero> arrayList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lv);
select sel=new select();//innter class take
sel.execute();
}
private class select extends AsyncTask {
ProgressDialog pg;
jsonclass js;
@Override
protected void onPreExecute() {
super.onPreExecute();
pg=new ProgressDialog(MainActivity.this);
pg.setTitle("Fetching....");
pg.setMessage("wait");
pg.setCancelable(true);
pg.setIndeterminate(false);
pg.show();
}
@Override
protected Object doInBackground(Object[] objects) {
js=new jsonclass();
result=js.processurl(URL_GATE_DATA);
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray h1 = jsonObj.getJSONArray("contacts");
for (int i = 0; i < h1.length(); i++) {
JSONObject c = h1.getJSONObject(i);
Hero h=new Hero();
h.setName(c.getString("name"));
h.setId(c.getString("id"));
h.setEmail(c.getString("email"));
h.setAddress(c.getString("address"));
h.setGender(c.getString("gender"));
// Phone node is JSON Object
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office");
h.setMobile(mobile);
h.setHome(home);
h.setOffice(office);
arrayList.add(h);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
if(pg.isShowing()){
pg.dismiss();
}
jsonAdapter jsonadapter=new jsonAdapter(MainActivity.this,arrayList);
lv.setAdapter(jsonadapter);
}
}
}
Step 2:build graddle(module:app)
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.squareup.picasso:picasso:2.71828'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
}
Step 3:Activiry_main_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.jay.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"></ListView>
</LinearLayout>
Step 4:simple_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:orientation="vertical"
android:layout_height="100dp">
<ImageView
android:id="@+id/iv"
android:background="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Step 5:Hero //hero java ma file bnavi hero itle pojjo jema value gete and set tay
public class Hero {
private String id;
private String name;
private String email;
private String address;
private String gender;
private String mobile;
private String home;
private String office;
//aatlu lkheya pache right click kari ne getter and setter karvu
Step 6:jsonAdapter //jsonadapter name ni java ma file bnaave
public class jsonAdapter extends BaseAdapter {
Context context;
ArrayList<Hero> arryList=new ArrayList<>();
public jsonAdapter(Context context, ArrayList<Hero> arryList) {
this.context = context;
this.arryList = arryList;
}
@Override
public int getCount() {
return arryList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.simple,null);
ImageView imageView;
TextView textView;
imageView=v.findViewById(R.id.iv);
textView=v.findViewById(R.id.tv);
Hero h=arryList.get(i);
textView.setText(h.getId() +"\n"+ h.getName() +"\n"+h.getEmail() +"\n"+ h.getAddress() +"\n"+h.getGender() +"\n"+h.getMobile() +"\n"+h.getHome() +"\n"+h.getOffice());
//Picasso.get().load(h.getImageurl()).into(imageView);
return v;
}
}
Step 7:json class
public class jsonclass {
String result;
public String processurl(String s)
{
try {
URL url=new URL(s);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code=conn.getResponseCode();
if (code==200)
{
InputStream in=new BufferedInputStream(conn.getInputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line="";
while ((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
result=sb.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
Step 8:MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView lv;
String URL_GATE_DATA="https://api.androidhive.info/contacts/",result;
ArrayList<Hero> arrayList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lv);
select sel=new select();//innter class take
sel.execute();
}
private class select extends AsyncTask {
ProgressDialog pg;
jsonclass js;
@Override
protected void onPreExecute() {
super.onPreExecute();
pg=new ProgressDialog(MainActivity.this);
pg.setTitle("Fetching....");
pg.setMessage("wait");
pg.setCancelable(true);
pg.setIndeterminate(false);
pg.show();
}
@Override
protected Object doInBackground(Object[] objects) {
js=new jsonclass();
result=js.processurl(URL_GATE_DATA);
try {
JSONObject jsonObj = new JSONObject(result);
JSONArray h1 = jsonObj.getJSONArray("contacts");
for (int i = 0; i < h1.length(); i++) {
JSONObject c = h1.getJSONObject(i);
Hero h=new Hero();
h.setName(c.getString("name"));
h.setId(c.getString("id"));
h.setEmail(c.getString("email"));
h.setAddress(c.getString("address"));
h.setGender(c.getString("gender"));
// Phone node is JSON Object
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office");
h.setMobile(mobile);
h.setHome(home);
h.setOffice(office);
arrayList.add(h);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
if(pg.isShowing()){
pg.dismiss();
}
jsonAdapter jsonadapter=new jsonAdapter(MainActivity.this,arrayList);
lv.setAdapter(jsonadapter);
}
}
}
Comments
Post a Comment