我正在爲我的課程開發一個Android項目。使用volley Android Studio獲取JSON響應
我必須得到JsonObject
來自兩個網站的回覆。
第一個是get_token
,當我將有效的用戶名和密碼解析到url中時,我將得到令牌數的json響應。
第二個是get_message
方法,我會從get_token
得到一個帶有令牌的祕密消息。我能夠成功獲得令牌,但我被困在獲取祕密消息。
我如何傳遞令牌?
這裏是我的主要活動代碼:
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();
}
}
感謝您的回覆。這是有道理的,我會盡力去解決它。 –