Parsing Objest Data Using Volley Library in Android App
Using this https://www.thorntech.com/2016/03/parsing-json-android-using-volley-library/ to learn how to use Volley to access and parse object data from an api get route.
When I install the apk and start the app, the screen gives me no parsed object from this api get route (single user) https://reqres.in/api/users/2
Any idea why? I've never used volley before but I read all weekend about it and it's easy to understand and implement but clearly I'm doing something wrong with pointing to the data or the response object.
I appreciate any help to point me in the right direction.
package com.volleythorntech.example;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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 {
// Will show the string "data" that holds the results
TextView results;
// URL of object to be parsed
String JsonURL = "https://reqres.in/api/users/2";
// This string will hold the results
String data = "";
// Defining the Volley request queue that handles the URL request concurrently
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 with id jsonData
results = (TextView) findViewById(R.id.jsonData);
// Creating the JsonObjectRequest class called obreq, passing required parameters:
//GET is used to fetch data from the server, JsonURL is the URL to be fetched from.
JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, JsonURL,
// The third parameter Listener overrides the method onResponse() and passes
//JSONObject as a parameter
new Response.Listener<JSONObject>() {
// Takes the response from the JSON request
@Override
public void onResponse(JSONObject response) {
try {
JSONObject obj = response.getJSONObject("data");
// Retrieves the string labeled "colorName" and "description" from
//the response JSON Object
//and converts them into javascript objects
String color = obj.getString("first_name");
String desc = obj.getString("last_name");
// Adds strings from object to the "data" string
data += "Color Name: " + color +
"nDescription : " + desc;
// Adds the data string to the TextView "results"
results.setText(data);
}
// 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();
}
}
},
// The final parameter overrides the method onErrorResponse() and passes VolleyError
//as a parameter
new Response.ErrorListener() {
@Override
// Handles errors that occur due to Volley
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
// Adds the JSON object request "obreq" to the request queue
requestQueue.add(obreq);
}
}
请检查清单中的许可
<uses-permission android:name="android.permission.INTERNET"/>
链接地址: http://www.djcxy.com/p/29510.html