所有。 我正在使用排球来向我的网站服务器发出http请求。当单击提交按钮向请求队列添加请求时,它工作正常并且响应。但再次单击该按钮不响应。它仍然显示progressdialog和响应没有。有什么不对吗?
以下是代码示例:
public class MainActivity extends ActionBarActivity {
private static final String URL = "http://www.google.com";
public RequestQueue mQueue = null;
public StringRequest request = null;
private Button mScanButton = null;
private TextView mDisplay = null;
private ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScanButton = (Button) findViewById(R.id.button_scan);
mDisplay = (TextView) findViewById(R.id.display);
request = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("Response retrieved");
mDisplay.setText(response);
dialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mDisplay.setText("That didn't work.");
dialog.dismiss();
}
});
request.setShouldCache(false);
mScanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this, "Mapping data", "please wait..");
mQueue = Volley.newRequestQueue(MainActivity.this);
mQueue.add(request);
System.out.println("Add to queue..");
}
});
}
答案 0 :(得分:0)
尝试this这里的指南可以帮助您轻松理解。
看这一行
if (ServerConfig.isNetworkOnline(SplashScreen.this)) {
StringRequest strReq = new StringRequest(Request.Method.POST, "your server link", serverResponse, errorListener) {
};
AppController.getInstance().getRequestQueue().add(strReq);
} else {
Toast.makeText(getApplicationContext(), "Please check your network connection", Toast.LENGTH_SHORT).show();
finish();
}
您可以使用以下代码删除上面的代码。
StringRequest strReq = new StringRequest(Request.Method.POST, "your server link", serverResponse, errorListener) {
};
AppController.getInstance().getRequestQueue().add(strReq);
答案 1 :(得分:0)
我尝试将请求声明区域移动到on-click方法,它工作正常。但我仍然不明白它是如何运作的。
public class MainActivity extends ActionBarActivity {
private static final String URL = "http://www.google.com";
public RequestQueue mQueue = null;
public StringRequest request = null;
private Button mScanButton = null;
private TextView mDisplay = null;
private ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScanButton = (Button) findViewById(R.id.button_scan);
mDisplay = (TextView) findViewById(R.id.display);
mScanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this, "Mapping data", "please wait..");
request = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("Response retrieved");
mDisplay.setText(response);
dialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mDisplay.setText("That didn't work.");
dialog.dismiss();
}
});
mQueue = Volley.newRequestQueue(MainActivity.this);
mQueue.add(request);
System.out.println("Add to queue..");
}
});
}