android studio上的我的请求队列没有';行不通
所以我有这样的请求队列,带有com.android.volley:volley:1.1.1和permission.INTERNETandroid studio上的我的请求队列没有';行不通,android,request-queueing,Android,Request Queueing,所以我有这样的请求队列,带有com.android.volley:volley:1.1.1和permission.INTERNET public TextView txt; public String text=""; private static final String url="http://192.168.100.7/diari/tampil_penyakit.php"; public RequestQueue requestQueue;
public TextView txt;
public String text="";
private static final String
url="http://192.168.100.7/diari/tampil_penyakit.php";
public RequestQueue requestQueue;
public StringRequest stringRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt);
requestQueue= Volley.newRequestQueue(MainActivity.this);
stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
text=response;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
text="Error";
}
});
requestQueue.add(stringRequest);
text=text+" empty";
txt.setText(text);
}
公共文本视图txt;
公共字符串text=“”;
私有静态最终字符串
url=”http://192.168.100.7/diari/tampil_penyakit.php";
公共请求队列请求队列;
公共字符串请求字符串请求;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView)findViewById(R.id.txt);
requestQueue=Volley.newRequestQueue(MainActivity.this);
stringRequest=newStringRequest(Request.Method.POST,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
文本=响应;
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
text=“错误”;
}
});
添加(stringRequest);
text=text+“空”;
txt.setText(文本);
}
但是txt只显示“empty”,我已经和postman一起尝试了url,有人能帮我改变一下你的回复吗
stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
text=response;
text=text+" empty";
txt.setText(text);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
text="Error";
}
stringRequest=newstringrequest(Request.Method.POST,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
文本=响应;
text=text+“空”;
txt.setText(文本);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
text=“错误”;
}
它不起作用,因为您基本上是在声明变量text,比如text=“”;然后设置队列,在设置之后,使用空变量设置textview的文本,而不等待服务器的响应。onresponse您不会更改textview中的文本。哇,谢谢,所以当我将txt.set放在requestqueue.add()下面时,它不会等待服务器的响应来设置文本?@IkiSangadji这就是请求的全部要点,代码不会等待它,因为它可能需要不确定的时间,并且必须冻结整个主线程。