JSON Parsing without Any Library
Note: In Android, we can/Should not write any network base code in the main thread/same thread, it'll through an exception when we will try to run the app.
we should not do anything / any code which can be the cause of being the slow main thread.
main thread means UI thread.
we can use AsyncTask<> class, which generate a different thread ( for working in the background thread).
and this class have a doInBackground method.
and this class have a doInBackground method.
Some Important Links for using :
http://json.parser.online.fr/
http://www.sercandemir.com/
Yahoo Weather API as location in JSON Formate
http://services.hanselandpetal.com/feeds/flowers.json
------------------------------------------------------------------------------------------------------------------------
JSON Parsing:
আমাদের JSON parsing করতে হলে ২ টা part নিয়ে কাজ করতে হবে ।
Part:1
Connection
Part:2
AsyncTask
[ কারন এটা (AsyncTask ) আমাদের কে মেইন থ্রেড থেকে অন্য থ্রেড এ নিয়ে যায়। যেখানে কিছু মেথড (method) overriding করলেই হবে। ]
But for some reason, we are doing Part:1 first
Part:2 AsyncTask
1.
---- make a private inner class within the main class and which extends by an
AsyncTask<String, Void, String>
----- and implement all the methods which need.
2.
------ Now write the below code for formatting JSON object to string ... into doInBackground method .
-- some terms: HTTP request, setrequestMethod, connect().
@Overrideprotected String doInBackground(String... params) { URL url = null; try { url = new URL(params[0]); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.connect(); //connect korar sathe sathe dataa asa suru hoye jabe //0/1 or binary data should collect in inputStream InputStream inputStream = httpURLConnection.getInputStream();
//lets Decode it
// utf-8 wise convert korbe
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"UTF-8"); //lets convert it as String //its easy with Buffer Reader BufferedReader bufferedReader = new BufferedReader(inputStreamReader); //Now take a little little and set as stringBuilder StringBuilder stringBuilder = new StringBuilder(); String line = ""; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); } return stringBuilder.toString(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } |
3. Now, onPostExecute we got the JSON Array.
-------- so here we should write some code to take the JSON array .
@Overrideprotected void onPostExecute(String s) { super.onPostExecute(s); try { JSONArray jsonArray = new JSONArray(s); JSONObject FlowerObject = jsonArray.getJSONObject(1); String FlowerName = FlowerObject.getString("name"); FlowerTV.setText(FlowerName); } catch (JSONException e) { e.printStackTrace(); } } |
Part:1 Connection
1. open your AndroidManifest.xml file -> and take permissions
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> . |
2. for
the connection we use ConnectivityManager.
the network check we use NetworkInfo.
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); |
then we check if the network is available and is Connected? if yes then call the Asynctask execute, with the URL as a parameter.
if(info.isAvailable() && info.isConnected()){
new MyJSONDownloadTask().execute(Flower_Url);
//execute() call er sathe sathe doInBackground() run hobe and paramtr nibe} else {} |
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন