JSON Volly url
MainActivity :-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Show item Activity :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/age"
android:textSize="20sp"/>
</LinearLayout>MainActivity Java :-package com.example.jsonurl;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
ListView listView;
String name,age;
String jsonurl= "https://run.mocky.io/v3/3fba2beb-4dde-4e03-853b-f6c19fa8fffd";
ArrayList<HashMap<String,String>> friends;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
friends = new ArrayList<>();
listView = findViewById(R.id.listview);
GetData getdata = new GetData();
getdata.execute();
}
public class GetData extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... strings) {
String current ="";
try {
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(jsonurl);
urlConnection =(HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
int data = inputStreamReader.read();
while (data!= -1){
current += (char) data;
data = inputStreamReader.read();
}
return current;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (urlConnection != null){
urlConnection.disconnect();
}
}
}catch (Exception e){
e.printStackTrace();
}
return current;
}
@Override
protected void onPostExecute(String s) {
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("Friends");
for (int i = 0 ; i<jsonArray.length(); i++){
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
name = jsonObject1.getString("name");
age = jsonObject1.getString("age");
HashMap <String,String>friend = new HashMap<>();
friend.put("name",name);
friend.put("age",age);
friends.add(friend);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
friends,
R.layout.showdata,
new String[]{"name","age"},
new int[]{R.id.name,R.id.age}
);
listView.setAdapter(adapter);
}
}
}
Comments
Post a Comment