I am working on an android project for my class.
I have to get JsonObject
response from two urls.
The first one is get_token
, where I will get a json response of token number when I parse a valid username and password into the url.
The second one is get_message
method where I will get a secret message with the token generated from get_token
. I was able to successfully get a token, but I am stuck at getting the secret message.
How do I pass the token?
Here is the code for my main activity:
private String urlJsonObj = "http://sfsuswe.com/413/get_token/?username=sahithiv&password=912549149";
private String urlJsonObj1="http://sfsuswe.com/413/get_message/?token=";
private static String TAG = MainActivity.class.getSimpleName();
private Button btnMakeObjectRequest;
ProgressDialog pDialog;
private TextView txtResponse;
private String jsonResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest);
txtResponse = (TextView) findViewById(R.id.txtResponse);
txtResponse.setMovementMethod(new ScrollingMovementMethod());
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
btnMakeObjectRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makeJsonObjectRequest();
}
});
}
/**
* Method to make json object request where json response starts wtih {
* */
private void makeJsonObjectRequest() {
showpDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
urlJsonObj, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
// Parsing json object response
// response will be a json object
String token = response.getString("token");
jsonResponse = "\n\n\n";
jsonResponse += "token:" + token + "\n\n\n\n";
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hidepDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
You need to pass Token appended with Next url.
String token = response.getString("token");
For Next url response:
String nextUrl = urlJsonObj1+token;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
nextUrl, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG+"Final Response", response.toString());
}
AppController.getInstance().addToRequestQueue(jsonObjReq);
Output would be:
{
"description": "CSC 413.02 Spring 2016 Project 2 Secret Message",
"message": "On the neighboring shore the fires from the foundry chimneys burning high and glaringly into the night,"
}
Hope this will heelp you.
Sorry I didnt read the question & code correctly.May be a wrong answer
I guess you are getting String token as null when outside the request.
I had a same issue when I wanted to use response outside the vollley request.
Create a separate class with
class store_response{
private static String token;
public static void set_token(String token_separated_from_response)
//to store the token
{
token=token_separated_from_response;
}
//for retrieving token
public static void get_token()
{
return token;
}
}
So when storing a response just call store_response.set_token(token_extracted_from_response);
And for retrieving outside the volley request. String token=store_response.get_token();
I'm posting this from mobile so sorry for not typing in code form.
You can solved this problem very simply....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.