In this tutorial we will learn about how to use volley for fetching the JSON results. If you are new to volley you can read our previous article, where we had discussed pretty much in detailed to start with volley.

In this article we will be creating an application (I called it as Mars Application) using the information collected by Curiosity rover on Mars. Use {MASS} API to build mobile weather apps. More info will be available here.

Project Set up: –

Start setting up new project in Android Studio I called it as MarsApplication. Hope you know how to integrate volley in Android Studio, if not, no problem you can refer our previous article.

    • Create a Java Class name – MarsRequest.java

The purpose of having this file is to create a singleton object of the RequestQueue. We have created MarsRequest class as a singleton class. And we have discussed in my previous article why we it is good to have single instance of Request Queue.
So my Class will look like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class MarsRequest {
 
    private RequestQueue requestQueue;
    private Context context;
    private static MarsRequest marsRequest;
 
 
 
    private MarsRequest(Context context){
        this.context = context;
        requestQueue = getRequestQueue();
    }
 
    public static MarsRequest getInstance(Context context){
        if(marsRequest == null){
            marsRequest = new MarsRequest(context);
        }
        return marsRequest;
    }
 
    public RequestQueue getRequestQueue() {
        if(requestQueue == null){
            requestQueue = Volley.newRequestQueue(context.getApplicationContext());
        }
        return requestQueue;
    }
 
    public void addToRequestQueue(Request request){
        getRequestQueue().add(request);
    }
}
    • Create Another Java name – MarsReport

This is the model class for the information that I am collecting from the JSON.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class MarsReport {
    private String terrestrialDate;
    private String marsDate;
    private String minTemp;
    private String maxTemp;
    private String windSpeed;
    private String pressure;
    private String weatherStatus;
 
    public String getTerrestrialDate() {
        return terrestrialDate;
    }
 
    public String getMarsDate() {
        return marsDate;
    }
 
    public String getMinTemp() {
        return minTemp;
    }
 
    public String getMaxTemp() {
        return maxTemp;
    }
 
    public String getWindSpeed() {
        return windSpeed;
    }
 
    public String getPressure() {
        return pressure;
    }
 
    public String getWeatherStatus() {
        return weatherStatus;
    }
 
    public void setTerrestrialDate(String terrestrialDate) {
        this.terrestrialDate = terrestrialDate;
    }
 
    public void setMarsDate(String marsDate) {
        this.marsDate = marsDate;
    }
 
    public void setMinTemp(String minTemp) {
        this.minTemp = minTemp;
    }
 
    public void setMaxTemp(String maxTemp) {
        this.maxTemp = maxTemp;
    }
 
    public void setWindSpeed(String windSpeed) {
        this.windSpeed = windSpeed;
    }
 
    public void setPressure(String pressure) {
        this.pressure = pressure;
    }
 
    public void setWeatherStatus(String weatherStatus) {
        this.weatherStatus = weatherStatus;
    }
}
    • Create a XML file name – activity_main

In this xml we will be having a button and text view and their respective values comes from the JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 <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"
    tools:context=".MainActivity">
 
    <Button
        android:id="@+id/MarsButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MarsWeather" />
 
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/MarsButton"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:stretchColumns="1">
 
        <TableRow>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TerrestrialDate" />
 
            <TextView
                android:id="@+id/tvTerrestrialDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="" />
        </TableRow>
 
        <TableRow>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mars Date" />
 
            <TextView
                android:id="@+id/tvMarsDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="" />
        </TableRow>
 
        <TableRow>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Min Temp" />
 
            <TextView
                android:id="@+id/tvMinTemp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="" />
        </TableRow>
 
        <TableRow>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Max Temp" />
 
            <TextView
                android:id="@+id/tvMaxTemp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="" />
        </TableRow>
 
        <TableRow>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Wind Speed" />
 
            <TextView
                android:id="@+id/tvWindSpeed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="" />
        </TableRow>
 
        <TableRow>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Weather Status" />
 
            <TextView
                android:id="@+id/tvweatherStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="" />
        </TableRow>
 
        <TableRow>
 
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pressure" />
 
            <TextView
                android:id="@+id/tvPressure"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="" />
        </TableRow>
 
    </TableLayout>
</RelativeLayout>
    • Last we have our MainActivity class, which has the responsibility of displaying parsing Json response to the screen.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 
    private TextView mTextTerrestrialDate;
    private TextView mTextMarsDate;
    private TextView mTextMinTemp;
    private TextView mTextMaxTemp;
    private TextView mTextWindSpeed;
    private TextView mTextPressure;
    private TextView mTextWeatherStatus;
    private Button btnMarsWeather;
    private ProgressDialog mProgressDialog;
 
    private static final String url = "http://marsweather.ingenology.com/v1/latest/";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getElementOfView();
        btnMarsWeather.setOnClickListener(this);
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setCancelable(false);
    }
 
    private void getElementOfView() {
        mTextTerrestrialDate = (TextView) findViewById(R.id.tvTerrestrialDate);
        mTextMarsDate = (TextView) findViewById(R.id.tvMarsDate);
        mTextMinTemp = (TextView) findViewById(R.id.tvMinTemp);
        mTextMaxTemp = (TextView) findViewById(R.id.tvMaxTemp);
        mTextWindSpeed = (TextView) findViewById(R.id.tvWindSpeed);
        mTextPressure = (TextView) findViewById(R.id.tvPressure);
        mTextWeatherStatus = (TextView) findViewById(R.id.tvweatherStatus);
        btnMarsWeather = (Button) findViewById(R.id.MarsButton);
    }
 
 
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.MarsButton:
                showDialog();
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
                        (Request.Method.GET, url, null, new Response.Listener() {
                            @Override
                            public void onResponse(JSONObject response) {
                                MarsReport marsReport = parseMarsJSONResponse(response);
                                mTextWeatherStatus.setText(marsReport.getWeatherStatus());
                                mTextPressure.setText(marsReport.getPressure());
                                mTextWindSpeed.setText(marsReport.getWindSpeed());
                                mTextMaxTemp.setText(marsReport.getMaxTemp());
                                mTextMinTemp.setText(marsReport.getMinTemp());
                                mTextMarsDate.setText(marsReport.getMarsDate());
                                mTextTerrestrialDate.setText(marsReport.getTerrestrialDate());
                                dismissDialog();
                            }
 
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                // TODO Auto-generated method stub
                                dismissDialog();
                            }
 
                        });
 
                MarsRequest.getInstance(this).addToRequestQueue(jsonObjectRequest);
 
                break;
        }
    }
 
    private MarsReport parseMarsJSONResponse(JSONObject response) {
        try {
            response = response.getJSONObject("report");
            MarsReport marsReport = new MarsReport();
            if (response.isNull("terrestrial_date"))
                marsReport.setTerrestrialDate("-");
            else
                marsReport.setTerrestrialDate(response.getString("terrestrial_date"));
            if (response.isNull("sol"))
                marsReport.setMarsDate("-");
            else
                marsReport.setMarsDate(response.getString("sol"));
            if (response.isNull("min_temp"))
                marsReport.setMinTemp("-");
            else
                marsReport.setMinTemp(response.getString("min_temp"));
            if (response.isNull("max_temp"))
                marsReport.setMaxTemp("-");
            else
                marsReport.setMaxTemp(response.getString("max_temp"));
            if (response.isNull("wind_speed"))
                marsReport.setWindSpeed("-");
            else
                marsReport.setWindSpeed(response.getString("wind_speed"));
            if (response.isNull("pressure"))
                marsReport.setPressure("-");
            else
                marsReport.setPressure(response.getString("pressure"));
            if (response.isNull("atmo_opacity"))
                marsReport.setWeatherStatus("-");
            else
                marsReport.setWeatherStatus(response.getString("atmo_opacity"));
            return marsReport;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
 
        return null;
    }
 
    private void showDialog() {
        if (!(mProgressDialog.isShowing())) {
            mProgressDialog.show();
        }
    }
 
    private void dismissDialog() {
        if (mProgressDialog.isShowing()) {
            mProgressDialog.hide();
        }
    }
}

Conclusion Volley: –

We covered decent enough to start and getting a basic understanding of Volley. We looked at the practical side of volley in this section.

Get the complete Sample here.

4 comments on “MarsApplication – Volley JSON Parsing

Leave a Reply

Your email address will not be published.