Skip to main content

JsonParsing Using AsyncTask in Android

 Create One ServiceHandler Class for Manage Connection For JsonParsing

ServiceHandler.java
public class ServiceHandler {

public String getstring(String url)
{
StringBuilder sb=new StringBuilder();
String result;
try{
URL url1=new URL(url);
HttpURLConnection con= (HttpURLConnection) url1.openConnection();
InputStream in= new BufferedInputStream(con.getInputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String line;
while ((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
}
catch (Exception e)
{

}
result=sb.toString();
return result;
}
}
public class MainActivity extends AppCompatActivity {

ListView lv;
String URLDATA="Enter URL";
String CONTACT="contacts";
String ID="id";
String NAME="name";
String EMAIL="email";
ArrayList<User>arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lstview);
arrayList=new ArrayList<>();
new Dataload().execute();
}
class Dataload extends AsyncTask<Void,Void,Void>
{
ProgressDialog pd;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(MainActivity.this);
pd.setTitle("Loading data");
pd.setMessage("Please Wait");
pd.show();
}

@Override
protected Void doInBackground(Void... voids) {
try{
ServiceHandler sh=new ServiceHandler();
String result=sh.getstring(URLDATA);
Log.e("Error",result);
if(result!=null)
{
JSONObject jsonObject=new JSONObject(result);
JSONArray jsonArray=jsonObject.getJSONArray(CONTACT);
for(int i=0;i<jsonArray.length();i++)
{
JSONObject obj=jsonArray.getJSONObject(i);
String id=obj.getString(ID);
String name=obj.getString(NAME);
String email=obj.getString(EMAIL);
User user=new User();
user.setId(id);
user.setName(name);
user.setEmail(email);
arrayList.add(user);
}
}
}
catch (Exception e)
{

}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pd.dismiss();
Customadpter customadpter = new Customadpter(MainActivity.this, arrayList);
lv.setAdapter(customadpter);
}
}
}

public class Customadpter extends BaseAdapter {
Context context;
ArrayList<User> arrayList;

public Customadpter(Context context, ArrayList<User> arrayList) {
this.context = context;
this.arrayList = arrayList;
}

@Override
public int getCount() {
return arrayList.size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom, null);
TextView tv1 = v.findViewById(R.id.textView2);
TextView tv2 = v.findViewById(R.id.textView3);
TextView tv3 = v.findViewById(R.id.textView4);
tv1.setText(arrayList.get(position).getId() + "");
tv2.setText(arrayList.get(position).getName());
tv3.setText(arrayList.get(position).getEmail());
return v;
}
}


public class User {
String id,name,email;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

<?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:id="@+id/lstview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>









Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"