简体   繁体   English

使用Volley获取JSON响应-Android Studio

[英]Getting JSON Response with volley- Android Studio

I am working on an android project for my class. 我正在为我的课程开发一个android项目。

I have to get JsonObject response from two urls. 我必须从两个URL获得JsonObject响应。

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. 第一个是get_token ,当我将有效的用户名和密码解析到url中时,将得到令get_token的json响应。
The second one is get_message method where I will get a secret message with the token generated from get_token . 第二个是get_message方法,其中我将获得一条秘密消息,其中包含从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. 您需要传递带有下一个 URL的令牌

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

For Next url response: 对于下一个 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);

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. 我想您在请求之外时将String令牌获取为null。

I had a same issue when I wanted to use response outside the vollley request. 当我想在vollley请求之外使用响应时,我遇到了同样的问题。

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); 因此,在存储响应时,只需调用store_response.set_token(token_extracted_from_response);

And for retrieving outside the volley request. 并用于排球以外的要求。 String token=store_response.get_token(); 字符串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.... 您可以非常简单地解决此问题...。

  1. Create two method one for get_token two for get_message 为get_token创建两个方法之一,为get_message创建两个方法
  2. First time call first method when you get response successfully then call your 2nd method by pass your token as parameter. 成功获得响应后,第一次调用第一个方法,然后通过传递令牌作为参数来调用第二个方法。 I check your api response and i think this is best solution for u. 我检查了您的api响应,我认为这是您的最佳解决方案。 thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM