我正在为我的班级开发一个 android 项目。
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();
}
}