Skip to content

Instantly share code, notes, and snippets.

@mnaraniya
Created September 6, 2017 07:20
Show Gist options
  • Save mnaraniya/033710719a62df9f27f4308e4682ad36 to your computer and use it in GitHub Desktop.
Save mnaraniya/033710719a62df9f27f4308e4682ad36 to your computer and use it in GitHub Desktop.
package com.example.android.jsonurl;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private TextView type, code, fbid;
String jsonUrl = "https://graph.facebook.com/me";
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates the Volley request queue
requestQueue = Volley.newRequestQueue(this);
// Casts results into the TextView found within the main layout XML
type = (TextView) findViewById(R.id.type);
code = (TextView) findViewById(R.id.code);
fbid = (TextView) findViewById(R.id.fbid);
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, jsonUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObj = response.getJSONObject("error");
String typeS = jsonObj.getString("type");
String codeS = jsonObj.getString("code");
String fbtrace_id = jsonObj.getString("fbtrace_id");
type.setText(typeS);
code.setText(codeS);
fbid.setText(fbtrace_id);
}
// Try and catch are included to handle any errors due to JSON
catch (JSONException e) {
// If an error occurs, this prints the error to the log
e.printStackTrace();
type.setText("error");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
requestQueue.add(jsObjRequest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment