Android 在截击JSONArray请求中传递参数
我正试图根据一个参数,即电子邮件地址,从服务器获取帖子。如何在JSONArrayRequest中发送参数,以便只检索与给定参数匹配的帖子?这是我的密码: JsonArrayRequest:Android 在截击JSONArray请求中传递参数,android,json,android-volley,Android,Json,Android Volley,我正试图根据一个参数,即电子邮件地址,从服务器获取帖子。如何在JSONArrayRequest中发送参数,以便只检索与给定参数匹配的帖子?这是我的密码: JsonArrayRequest: JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { @Override public void onRe
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for(int i= 0; i<response.length(); i++){
try {
JSONObject object = response.getJSONObject(i);
Post post = new Post();
post.setContent(object.getString("Content"));
post.setDate(object.getString("PostDate"));
post.setTime(object.getString("PostTime"));
postArray.add(post);
PostList.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getmInstance().addToRequestQueue(jsonArrayRequest);
JsonArrayRequest JsonArrayRequest=newjsonarrayrequest(url,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
对于(int i=0;ivoid MakePostRequest(){
StringRequest postRequest=新的StringRequest(Request.Method.POST,EndPoints.BASE\u URL\u ADS,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject jsonResponse=新的JSONObject(响应);
value1=jsonResponse.getString(“您的ID1”);
value2=jsonResponse.getString(“您的ID2”);
}捕获(JSONException e){
e、 printStackTrace();
banner_id=null;
完整id=空;
}
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
value1=null;
value2=null;
}
}
) {
//以下是参数将使用post方法添加到您的url
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“app”,getString(R.string.app_name));
//参数put(“2ndParamName”、“valueoF2ndParam”);
返回参数;
}
};
newRequestQueue(this.add)(postRequest);
}
此post请求使用的是compile'com.mcxiaoke.volley:library:1.0.19'
volley版本
我只是添加了app name作为参数。你可以添加更多参数。你想使用get还是post?post方法。我对Android有点陌生。所以我猜post方法比get更好。这正是我需要的。非常感谢。我能在onResponse()中使用JsonArray代替JsonObject吗函数?@Sohail Zahid为什么我们不能使用JsonArrayRequest
public class AppController extends Application {
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
public static final String TAG = AppController.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getmInstance() {
return mInstance;
}
public RequestQueue getmRequestQueue()
{
if(mRequestQueue == null){
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getmImageLoader(){
getmRequestQueue();
if(mImageLoader == null){
mImageLoader = new ImageLoader(this.mRequestQueue, new BitmapCache());
}
return mImageLoader;
}
public <T> void addToRequestQueue(Request<T> request, String tag ){
request.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getmRequestQueue(). add(request);
}
public <T> void addToRequestQueue(Request<T> request ){
request.setTag(TAG);
getmRequestQueue(). add(request);
}
public void cancelPendingRequest(Object tag){
if(mRequestQueue != null){
mRequestQueue.cancelAll(tag);
}
}
void MakePostRequest() {
StringRequest postRequest = new StringRequest(Request.Method.POST, EndPoints.BASE_URL_ADS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
value1= jsonResponse.getString("Your ID1");
value2= jsonResponse.getString("Your ID2");
} catch (JSONException e) {
e.printStackTrace();
banner_id = null;
full_id = null;
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
value1= null;
value2= null;
}
}
) {
// here is params will add to your url using post method
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("app", getString(R.string.app_name));
//params.put("2ndParamName","valueoF2ndParam");
return params;
}
};
Volley.newRequestQueue(this).add(postRequest);
}