build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.samset.jsonparsingexample"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.samset.jsonparsingexample"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
}
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samset.jsonparsingexample.MainActivity">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_weight="10"
android:visibility="gone"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:visibility="invisible"
android:id="@+id/container"
android:layout_weight="60"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/device_image"
/>
<TableLayout
android:paddingTop="10dp"
android:id="@+id/table"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Device:"
android:layout_weight="1"
android:textSize="@dimen/abc_text_size_title_material"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:id="@+id/device"
android:textSize="@dimen/abc_text_size_title_material"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="OS:"
android:layout_weight="1"
android:textSize="@dimen/abc_text_size_title_material"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:id="@+id/os"
android:textSize="@dimen/abc_text_size_title_material"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Chipset:"
android:layout_weight="1"
android:textSize="@dimen/abc_text_size_title_material"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:id="@+id/chipset"
android:textSize="@dimen/abc_text_size_title_material"
/>
</TableRow>
</TableLayout>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/container"
android:textColor="#000"
android:text="Get Data"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.samset.jsonparsingexample.MainActivity">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_weight="10"
android:visibility="gone"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:visibility="invisible"
android:id="@+id/container"
android:layout_weight="60"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/device_image"
/>
<TableLayout
android:paddingTop="10dp"
android:id="@+id/table"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Device:"
android:layout_weight="1"
android:textSize="@dimen/abc_text_size_title_material"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:id="@+id/device"
android:textSize="@dimen/abc_text_size_title_material"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="OS:"
android:layout_weight="1"
android:textSize="@dimen/abc_text_size_title_material"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:id="@+id/os"
android:textSize="@dimen/abc_text_size_title_material"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Chipset:"
android:layout_weight="1"
android:textSize="@dimen/abc_text_size_title_material"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:id="@+id/chipset"
android:textSize="@dimen/abc_text_size_title_material"
/>
</TableRow>
</TableLayout>
</LinearLayout>
<Button
android:id="@+id/btn"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/container"
android:textColor="#000"
android:text="Get Data"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActvity.java
package com.samset.jsonparsingexample;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private String URL="http://data.d-codepages.com/nexus.json";
private TextView device,os,chipset;
private ProgressBar progress;
private ImageView device_image;
private LinearLayout layout;
private Button btnget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView()
{
layout = (LinearLayout) findViewById(R.id.container);
device = (TextView) findViewById(R.id.device);
os = (TextView) findViewById(R.id.os);
device_image = (ImageView) findViewById(R.id.device_image);
chipset = (TextView) findViewById(R.id.chipset);
progress = (ProgressBar) findViewById(R.id.progressBar);
btnget=(Button)findViewById(R.id.btn);
btnget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ParseJson().execute();
}
});
}
private void setData(String strdata)
{
if (strdata!=null)
{
JSONObject jsonObject=null;
String imageurl=null;
try {
jsonObject=new JSONObject(strdata);
if (jsonObject!=null)
{
progress.setVisibility(View.GONE);
device.setText( jsonObject.getString("Device"));
os.setText(jsonObject.getString("OS"));
chipset.setText(jsonObject.getString("Chipset"));
imageurl = jsonObject.getString("image");
Picasso.with(MainActivity.this).load(imageurl).into(device_image);
layout.setVisibility(View.VISIBLE);
}
}catch (JSONException ex)
{
ex.printStackTrace();
}
}
}
public class ParseJson extends AsyncTask<String,String,String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
String data=JsonParser.getDataFromserver(URL);
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s!=null)
{
setData(s);
}
}
}
}
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private String URL="http://data.d-codepages.com/nexus.json";
private TextView device,os,chipset;
private ProgressBar progress;
private ImageView device_image;
private LinearLayout layout;
private Button btnget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView()
{
layout = (LinearLayout) findViewById(R.id.container);
device = (TextView) findViewById(R.id.device);
os = (TextView) findViewById(R.id.os);
device_image = (ImageView) findViewById(R.id.device_image);
chipset = (TextView) findViewById(R.id.chipset);
progress = (ProgressBar) findViewById(R.id.progressBar);
btnget=(Button)findViewById(R.id.btn);
btnget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ParseJson().execute();
}
});
}
private void setData(String strdata)
{
if (strdata!=null)
{
JSONObject jsonObject=null;
String imageurl=null;
try {
jsonObject=new JSONObject(strdata);
if (jsonObject!=null)
{
progress.setVisibility(View.GONE);
device.setText( jsonObject.getString("Device"));
os.setText(jsonObject.getString("OS"));
chipset.setText(jsonObject.getString("Chipset"));
imageurl = jsonObject.getString("image");
Picasso.with(MainActivity.this).load(imageurl).into(device_image);
layout.setVisibility(View.VISIBLE);
}
}catch (JSONException ex)
{
ex.printStackTrace();
}
}
}
public class ParseJson extends AsyncTask<String,String,String>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
progress.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
String data=JsonParser.getDataFromserver(URL);
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s!=null)
{
setData(s);
}
}
}
}
JsonParser.java
package com.samset.jsonparsingexample;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
/**
* Created by samset on 20/05/16.
*/
public class JsonParser {
private static OkHttpClient okHttpClient;
private static String strdata;
public static String getDataFromserver(String url) {
okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
strdata = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return strdata;
}
}
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
/**
* Created by samset on 20/05/16.
*/
public class JsonParser {
private static OkHttpClient okHttpClient;
private static String strdata;
public static String getDataFromserver(String url) {
okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
strdata = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return strdata;
}
}
Thank you
Live sample
No comments:
Post a Comment