In this blog post, ill show you how to Retrieving JSON data and Displaying it in Android TextView.
My Other blog post on JSON Tutorial.
- Part I – What is JSON and how to code JSON data on Php
- Part II – How to retrieve a simplest JSON data in Android
- Part III – How to retrieve a JSON data to ListView in Android
- Part IV – How to retrieve a complex typed JSON data in Android
- Part V – How to retrieve a JSON data from a Secured Site with Basic Authentication
- Part VI – How to Pass a JSON data to Php as a Parameters
Please read Part I of this blog, there you can create JSON data in php. I’m using Example 1 from that blog in this tutorial for making this blog to be simple. Hope you have created JSON data in php, so lets get started with retrieving in Android.
Step 1: Create a new project File -> Android Project. While creating a new project give activity name as MainActivity and copy paste this code.
1 |
import org.json.JSONException; |
2 |
import org.json.JSONObject; |
4 |
import android.app.Activity; |
5 |
import android.app.ProgressDialog; |
6 |
import android.os.AsyncTask; |
7 |
import android.os.Bundle; |
8 |
import android.widget.TextView; |
10 |
public class MainActivity extends Activity { |
11 |
/** Called when the activity is first created. */ |
18 |
public void onCreate(Bundle savedInstanceState) { |
19 |
super.onCreate(savedInstanceState); |
20 |
setContentView(R.layout.activity_main); |
22 |
new JSONParse().execute(); |
25 |
private class JSONParse extends AsyncTask<String, String, JSONObject> { |
26 |
private ProgressDialog pDialog; |
28 |
protected void onPreExecute() { |
30 |
tv1 = (TextView) findViewById(R.id.textView1); |
32 |
pDialog = new ProgressDialog(MainActivity.this); |
33 |
pDialog.setMessage("Loading Data ..."); |
34 |
pDialog.setIndeterminate(false); |
35 |
pDialog.setCancelable(true); |
37 |
tv1.setText("Getting Values Pls wait.."); |
41 |
protected JSONObject doInBackground(String... args) { |
42 |
JsonParser jParser = new JsonParser(); |
43 |
// Getting JSON from URL |
44 |
JSONObject json = jParser.getJSONFromUrl(url); |
49 |
protected void onPostExecute(JSONObject json) { |
52 |
name = json.getString("name"); |
53 |
version = json.getString("version"); |
54 |
tv1.setText(name + " - " + version); |
55 |
} catch (JSONException e) { |
Step 2: Create a new class, JsonParser.java and copy paste this code.
1 |
import java.io.BufferedReader; |
2 |
import java.io.IOException; |
3 |
import java.io.InputStream; |
4 |
import java.io.InputStreamReader; |
5 |
import java.io.UnsupportedEncodingException; |
7 |
import org.apache.http.HttpEntity; |
8 |
import org.apache.http.HttpResponse; |
9 |
import org.apache.http.client.ClientProtocolException; |
10 |
import org.apache.http.client.methods.HttpPost; |
11 |
import org.apache.http.impl.client.DefaultHttpClient; |
12 |
import org.json.JSONException; |
13 |
import org.json.JSONObject; |
15 |
import android.util.Log; |
17 |
public class JsonParser { |
18 |
static InputStream is = null; |
19 |
static JSONObject jObj = null; |
20 |
static String json = ""; |
26 |
public JSONObject getJSONFromUrl(String url) { |
27 |
// Making HTTP request |
30 |
DefaultHttpClient httpClient = new DefaultHttpClient(); |
31 |
HttpPost httpPost = new HttpPost(url); |
32 |
HttpResponse httpResponse = httpClient.execute(httpPost); |
33 |
HttpEntity httpEntity = httpResponse.getEntity(); |
34 |
is = httpEntity.getContent(); |
35 |
} catch (UnsupportedEncodingException e) { |
37 |
} catch (ClientProtocolException e) { |
39 |
} catch (IOException e) { |
43 |
BufferedReader reader = new BufferedReader(new InputStreamReader( |
44 |
is, "iso-8859-1"), 8); |
45 |
StringBuilder sb = new StringBuilder(); |
47 |
while ((line = reader.readLine()) != null) { |
48 |
sb.append(line + "n"); |
52 |
} catch (Exception e) { |
53 |
Log.e("Buffer Error", "Error converting result " + e.toString()); |
55 |
// try parse the string to a JSON object |
57 |
jObj = new JSONObject(json); |
58 |
} catch (JSONException e) { |
59 |
Log.e("JSON Parser", "Error parsing data " + e.toString()); |
Step 3: Open your activity_main.xml and copy paste this code:
3 |
android:layout_width="match_parent" |
4 |
android:layout_height="match_parent" |
5 |
android:paddingBottom="@dimen/activity_vertical_margin" |
6 |
android:paddingLeft="@dimen/activity_horizontal_margin" |
7 |
android:paddingRight="@dimen/activity_horizontal_margin" |
8 |
android:paddingTop="@dimen/activity_vertical_margin" > |
11 |
android:id="@+id/textView1" |
12 |
android:layout_width="wrap_content" |
13 |
android:layout_height="wrap_content" |
14 |
android:layout_centerHorizontal="true" |
15 |
android:layout_centerVertical="true" |
16 |
android:text="Medium Text" |
17 |
android:textAppearance="?android:attr/textAppearanceMedium" /> |
Step 4: Open your AndroidManifest.xml and add Internet Permission.
1 |
<?xml version="1.0" encoding="utf-8"?> |
3 |
package="com.geeks.gallery.json_demoexample" |
4 |
android:versionCode="1" |
5 |
android:versionName="1.0" > |
8 |
android:minSdkVersion="16" |
9 |
android:targetSdkVersion="19" /> |
10 |
<uses-permission android:name="android.permission.INTERNET"/> |
11 |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> |
14 |
android:allowBackup="true" |
15 |
android:icon="@drawable/ic_launcher" |
16 |
android:label="@string/app_name" |
17 |
android:theme="@style/AppTheme" > |
19 |
android:name=".MainActivity" |
20 |
android:label="@string/app_name" > |
22 |
<action android:name="android.intent.action.MAIN" /> |
24 |
<category android:name="android.intent.category.LAUNCHER" /> |
ScreenShot:
Download Source Code from here.
[출처] http://www.geeks.gallery/simple-json-parsing-example-in-android-part-ii/