gpt4 book ai didi

java - 使用 volley 获取 JSON 响应 - Android Studio

转载 作者:太空宇宙 更新时间:2023-11-04 12:52:34 25 4
gpt4 key购买 nike

我正在为我的类(class)开发一个 Android 项目。

我必须从两个网址获取 JsonObject 响应。

第一个是 get_token,当我将有效的用户名和密码解析到 URL 中时,我将获得 token 编号的 json 响应。
第二个是 get_message 方法,我将使用 get_token 生成的 token 获取一条 secret 消息。我能够成功获取 token ,但我无法获取 secret 消息。
如何传递 token ?

这是我的主要 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();

}

}

最佳答案

您需要传递附加有下一步网址的 token

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

对于下一个网址响应:

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

希望这对你有帮助。

关于java - 使用 volley 获取 JSON 响应 - Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35678867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com