簡體   English   中英

使用Volley獲取JSON響應-Android Studio

[英]Getting JSON Response with volley- Android Studio

我正在為我的課程開發一個android項目。

我必須從兩個URL獲得JsonObject響應。

第一個是get_token ,當我將有效的用戶名和密碼解析到url中時,將得到令get_token的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();

}

}

您需要傳遞帶有下一個 URL的令牌

String token = response.getString("token");

對於下一個 URL響應:

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);

輸出為:

{
    "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,"
}

希望這會助你一臂之力。

抱歉,我沒有正確閱讀問題和代碼。可能是錯誤的答案

我想您在請求之外時將String令牌獲取為null。

當我想在vollley請求之外使用響應時,我遇到了同樣的問題。

使用創建一個單獨的類

  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;
}
}

因此,在存儲響應時,只需調用store_response.set_token(token_extracted_from_response);

並用於排球以外的要求。 字符串token = store_response.get_token();

我正在通過移動設備發布此消息,因此很抱歉沒有以代碼形式輸入。

您可以非常簡單地解決此問題...。

  1. 為get_token創建兩個方法之一,為get_message創建兩個方法
  2. 成功獲得響應后,第一次調用第一個方法,然后通過傳遞令牌作為參數來調用第二個方法。 我檢查了您的api響應,我認為這是您的最佳解決方案。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM