【问题标题】:Getting JSON Response with volley- Android Studio使用凌空获取 JSON 响应 - Android Studio
【发布时间】:2016-02-28 04:55:29
【问题描述】:

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

}

}

【问题讨论】:

    标签: java android android-volley jsonobject jsonobjectrequest


    【解决方案1】:

    您需要传递 Token 并附加 Next 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,"
    }
    

    希望这对你有帮助。

    【讨论】:

    • 感谢您的回复。有道理。我会努力解决的。
    【解决方案2】:

    对不起,我没有正确阅读问题和代码。可能是一个错误的答案

    我猜你在请求之外得到的字符串令牌为空。

    当我想在 volllley 请求之外使用响应时,我遇到了同样的问题。

    创建一个单独的类
      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);

    用于在截击请求之外检索。 String token=store_response.get_token();

    我是从手机上发布的,很抱歉没有输入代码表单。

    【讨论】:

      【解决方案3】:

      你可以很简单的解决这个问题......

      1. 为get_token 创建两个方法一为get_message 创建两个方法
      2. 当您成功获得响应时,第一次调用第一个方法,然后通过传递您的令牌作为参数调用您的第二个方法。我检查了你的 api 响应,我认为这对你来说是最好的解决方案。谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-25
        • 1970-01-01
        • 2017-10-21
        • 2021-06-24
        • 2020-09-12
        • 1970-01-01
        • 1970-01-01
        • 2018-08-26
        相关资源
        最近更新 更多