如何在 android studio 中 return 或重用 Volley 库的响应值?
How to return or reuse response value of Volley library in android studio?
public class MainActivity extends AppCompatActivity {
private static final String url = "https://www.googleapis.com/books/v1/volumes?q=android&maxResults=1";
private static String data ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final StringRequest request = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
data=data + response.substring(0);
Log.d("CODE1",response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
TextView text = (TextView) findViewById(R.id.text_view);
text.setText(data);
}
}
我正在尝试在 onCreate 函数末尾使用 onResponse 函数的“response”值。如您所见,数据已分配给 TEXTVIEW,但未显示。我们如何使用response中的数据传递给其他数据结构,然后用于其他工作?
这样使用:
data=data + response.substring(0);
Log.d("CODE1",response);
text.setText(data);
因为,此行 (setText) 在您发送请求之前运行。您显示了空数据。
将setText
方法移动到onResponse
您的代码将是这样的:
TextView text = (TextView) findViewById(R.id.text_view);
final StringRequest request = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("CODE1",response);
text.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
public class MainActivity extends AppCompatActivity {
private static final String url = "https://www.googleapis.com/books/v1/volumes?q=android&maxResults=1";
private static String data ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final StringRequest request = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
data=data + response.substring(0);
Log.d("CODE1",response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
TextView text = (TextView) findViewById(R.id.text_view);
text.setText(data);
}
}
我正在尝试在 onCreate 函数末尾使用 onResponse 函数的“response”值。如您所见,数据已分配给 TEXTVIEW,但未显示。我们如何使用response中的数据传递给其他数据结构,然后用于其他工作?
这样使用:
data=data + response.substring(0);
Log.d("CODE1",response);
text.setText(data);
因为,此行 (setText) 在您发送请求之前运行。您显示了空数据。
将setText
方法移动到onResponse
您的代码将是这样的:
TextView text = (TextView) findViewById(R.id.text_view);
final StringRequest request = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("CODE1",response);
text.setText(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Something went wrong",Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);