注释部分代码,可用
package com.example.xmskf.learnvolley;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "http://192.168.1.222:8080/fangdong2/NewsListServlet";
final ProgressDialog progressDialog = ProgressDialog.show(this, "this is title", "....load");
RequestQueue mQueue = Volley.newRequestQueue(this);
// //StringRequest Get方式
// StringRequest stringRequest = new StringRequest("http://www.baidu.com",
// new Response.Listener<String>() {
// @Override
// public void onResponse(String response) {
// Log.d("TAG", response);
// }
// },
// new Response.ErrorListener() {
// @Override
// public void onErrorResponse(VolleyError error) {
// Log.e("TAG", error.getMessage(), error);
// }
// });
//
//JsonObjectRequest Post方式
// JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
// Request.Method.POST,
// url,
// null,
// new Response.Listener<JSONObject>() {
// @Override
// public void onResponse(JSONObject jsonObject) {
// System.out.println("jsonObject="+jsonObject);
// if (progressDialog.isShowing()&&progressDialog!=null){
// progressDialog.dismiss();
// }
// }
// },
// new Response.ErrorListener(){
// @Override
// public void onErrorResponse(VolleyError volleyError) {
// System.out.println("error\n");
// System.out.println(volleyError);
// }
// });
Response.Listener<JSONArray> listener =new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
System.out.println("jsonArray="+jsonArray);
if (progressDialog.isShowing()&&progressDialog!=null){
progressDialog.dismiss();
}
}
};
Response.ErrorListener errorListener =new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println("error\n");
System.out.println(volleyError);
}
};
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(url, listener,errorListener){};
mQueue.add(jsonArrayRequest);
// getJSONByVolley();
}
}