JSON data parsing Showing into LISTVIEW
AndroidManifest.Xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.oshin.myjsonlist"> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Main_activity.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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="com.example.oshin.myjsonlist.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:dividerHeight="3dp" android:divider="@color/colorPrimaryDark" android:id="@+id/l1"></ListView> </LinearLayout>sample.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" > <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name" android:textAlignment="center" android:textSize="30sp" android:textStyle="bold" android:background="@color/colorAccent" /> <TextView android:id="@+id/grade" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ea8e8e" android:text="Grade" android:textAlignment="center" android:textSize="25sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:textStyle="bold" android:text="Section" android:id="@+id/section" android:textAlignment="center" android:background="#c29ef5" /> </LinearLayout>Customadapter.javapackage com.example.oshin.myjsonlist; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; /** * Created by oshin on 1/30/2018. */ class CustomAdapter extends BaseAdapter{ private Context applicationContext; private int sample; private List<JsonModel> jsonModels; CustomAdapter(Context applicationContext, int sample, List<JsonModel> jsonModels) { this.applicationContext =applicationContext; this.sample = sample; this.jsonModels =jsonModels; } @Override public int getCount() { return jsonModels.size(); } @Override public Object getItem(int i) { return jsonModels.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if(view == null) { LayoutInflater layoutInflater = (LayoutInflater) applicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.sample,viewGroup,false); } TextView name,grade,section; name= view.findViewById(R.id.name); grade=view.findViewById(R.id.grade); section=view.findViewById(R.id.section); name.setText(jsonModels.get(i).getName()); grade.setText(jsonModels.get(i).getGrade()); section.setText(jsonModels.get(i).getSection()); return view; } }jsonModel.javapackage com.example.oshin.myjsonlist; /** * Created by oshin on 1/30/2018. */ public class JsonModel { private String name; private String grade; private String section; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } public String getSection() { return section; } public void setSection(String section) { this.section = section; } }MainActivity.javapackage com.example.oshin.myjsonlist; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; 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.List; public class MainActivity extends AppCompatActivity { private ListView l1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); l1 = (ListView) findViewById(R.id.l1); JsonWork jsonWork = new JsonWork(); jsonWork.execute(); } private class JsonWork extends AsyncTask<String,String,List<JsonModel>>{ HttpURLConnection httpURLConnection = null; BufferedReader bufferedReader = null; String FullJsonData; @Override protected List<JsonModel> doInBackground(String... strings) { try { URL url = new URL("https://api.myjson.com/bins/17ab25"); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); InputStream inputStream = httpURLConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer stringBuffer= new StringBuffer(); String line=""; while ((line= bufferedReader.readLine())!= null){ stringBuffer.append(line); } FullJsonData = stringBuffer.toString(); List<JsonModel> jsonModelList = new ArrayList<>(); JSONObject jsonStartingObject = new JSONObject(FullJsonData); JSONArray jsonStudentArray = jsonStartingObject.getJSONArray("result"); for(int i=0; i<jsonStudentArray.length(); i++) { JSONObject jsonUnderArrayObject = jsonStudentArray.getJSONObject(i); JsonModel jsonModel = new JsonModel(); jsonModel.setName(jsonUnderArrayObject.getString("name")); jsonModel.setGrade(jsonUnderArrayObject.getString("grade")); jsonModel.setSection(jsonUnderArrayObject.getString("section")); jsonModelList.add(jsonModel); } return jsonModelList; } catch (JSONException | IOException e) { e.printStackTrace(); } finally{ httpURLConnection.disconnect(); try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } @Override protected void onPostExecute(List<JsonModel> jsonModels) { super.onPostExecute(jsonModels); CustomAdapter adapter = new CustomAdapter(getApplicationContext(),R.layout.sample,jsonModels); l1.setAdapter(adapter); } } }<script src="https://gist.github.com/TasnuvaOshin/0e9a2518c09ed83463a864907d03b482.js"></script>
Please give the zip file.. I Like to learn and work like this.
ReplyDeleteThank you for your tultorial.