Java 类com.android.volley.toolbox.JsonRequest 实例源码

项目:Gizwits-SmartBuld_Android    文件:GosScheduleSiteTool.java   
private void sendDateToSite(String httpurl, JSONObject jsonObject, final OnResponListener respon) {
    JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Method.POST, httpurl, jsonObject,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i("onSite", "response -> " + response.toString());
                    respon.OnRespon(0, response.optString("id"));
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    respon.OnRespon(1, error.toString());
                    error.printStackTrace();
                    Log.i("onSite", "sendDateToSite请求失败" + error.toString());
                }
            }) {

        @Override
        public Map<String, String> getHeaders() {
            return getHeaderWithToken();
        }
    };
    mRequestQueue.add(jsonRequest);
}
项目:pimatic-android    文件:RestClient.java   
public JsonRequest<JSONObject> createParamRequest(final int method, final String url,
                                                  final Map<String, String> params,
                                                  final Response.Listener<JSONObject> onResponse,
                                                  final Response.ErrorListener onError) {
    String queryString = UrlQueryString.buildQueryString(params);
    String fullUrl = baseUrl + url + (queryString.length() > 0 ? "?" + queryString : "");
    JsonObjectRequest request = new JsonObjectRequest(method, fullUrl, onResponse, onError) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return headers;
        }

        @Override
        public Map<String, String> getParams() {
            Log.v("getParams", params.toString());
            return params;
        }

    };
    request.setShouldCache(false);
    return request;
}
项目:pimatic-android    文件:RestClient.java   
public JsonRequest<JSONObject> getMessages() {
    HashMap<String, String> params = new HashMap<String, String>();
    JsonRequest<JSONObject> request = createParamRequest(Request.Method.GET, "/api/database/messages", params, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jsonObject) {
            Log.v("RestClient", jsonObject.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.e("RestClient", volleyError.getMessage());
        }
    });
    queue.add(request);
    return request;
}
项目:ktball    文件:UploadService.java   
private void initData() {
    long id = PreferenceManager.getDefaultSharedPreferences(UploadService.this).getLong(LoginActivity.PRE_CURRENT_USER_ID, 0);
    String club_id = String.valueOf(id);
    String url = Constants.KTHOST + "users/get_role?user_id=" +
            club_id + "&authenticity_token=" + MD5.getToken(Constants.KTHOST + "users/get_role");
    JsonRequest jsonRequest = new JsonObjectRequest(url,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    Token token = new Gson().fromJson(jsonObject.toString(), new TypeToken<Token>() {
                    }.getType());
                    access_token = token.youku_token;
                    Log.d("access_token", access_token);

                    doUp(list.get(current));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
        }
    }) {
        @Override
        public Map<String, String> getHeaders() {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json; charset=UTF-8");
            return headers;
        }
    };
    VolleyUtil.getInstance(getApplicationContext()).addRequest(jsonRequest);
}
项目:ktball    文件:ClubDataActivity3.java   
private void test1() {
    Log.d("soulnq", "请求网络");
    String url = Constants.KTHOST+"shool_user_tests/get_football_skills";
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("authenticity_token", "f3cc6394ca4696dab16061800aacd2d8");
        Log.d("soulnq", jsonObject.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonRequest<JSONObject> request = new JsonObjectRequest(com.android.volley.Request.Method.GET,
            url, jsonObject, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            ffff userMsg = new Gson().fromJson(response.toString(),new TypeToken<ffff>(){}.getType());
            if (userMsg != null) {
                Toast.makeText(ClubDataActivity3.this, "成功", Toast.LENGTH_LONG).show();
                Toast.makeText(ClubDataActivity3.this, "数据:" + userMsg.toString(), Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(ClubDataActivity3.this, "失败", Toast.LENGTH_LONG).show();
            }
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(ClubDataActivity3.this, "失败", Toast.LENGTH_LONG).show();
        }
    })
    {
        @Override
        public Map<String, String> getHeaders() {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json; charset=UTF-8");
            return headers;
        }
    };
    VolleyUtil.getInstance(getApplicationContext()).addRequest(request);
}
项目:ktball    文件:NewGamesActivity.java   
@Override
protected void onStart() {//从服务器获取数据
    super.onStart();
    long uer_id = PreferenceManager.getDefaultSharedPreferences(this).getLong(LoginActivity.PRE_CURRENT_USER_ID,0);
    String id = String.valueOf(uer_id);
    String url = Constants.KTHOST+"games/new?user_id=" + id +
            "&authenticity_token="+MD5.getToken(Constants.KTHOST+"games/new");
    JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(
            url,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    LogUtils.e(jsonObject.toString());
                    gsonAnalysis(jsonObject.toString());
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.d("volleyError=====",volleyError.toString());
        }
    }){

        @Override
        public Map<String, String> getHeaders() {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json; charset=UTF-8");
            return headers;
        }
    };
    VolleyUtil.getInstance(this).addRequest(jsonRequest);
}
项目:FindMeAHome    文件:LocationFragment.java   
private void addRequest(String url) {
    JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONObject location = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
                double latitude = location.getDouble("lat");
                double longitude = location.getDouble("lng");

                LatLng latLng = new LatLng(latitude, longitude);

                goToLocation(latLng);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    requestQueue.add(jsonRequest);
}
项目:pimatic-android    文件:RestClient.java   
public JsonRequest<JSONObject> login(String username, String password,
                                     Response.Listener<JSONObject> onResponse,
                                     Response.ErrorListener onError) {
    JSONObject params = new JSONObject();
    try {
        params.put("username", username);
        params.put("password", password);
    } catch (Exception e) {
        e.printStackTrace();
    }
    JsonRequest<JSONObject> request = createJsonRequest(Request.Method.POST, "/login", params, onResponse, onError);
    queue.add(request);
    return request;
}
项目:pimatic-android    文件:RestClient.java   
public JsonRequest<JSONObject> callDeviceAction(Device device, String actionName, Map<String, String> params,
                                                Response.Listener<JSONObject> onResponse,
                                                Response.ErrorListener onError) {

    JsonRequest<JSONObject> request = createParamRequest(
            Request.Method.GET,
            "/api/device/" + device.getId() + "/" + actionName,
            params,
            onResponse,
            onError
    );
    queue.add(request);
    return request;
}
项目:OSMonitor    文件:HttpUtil.java   
public void addRequest(JsonRequest<?> newRequest) {

    if (newRequest == null)
      return;

    newRequest.setTag(TAG);
    mRequestQueue.add(newRequest);
    return;
  }
项目:ktball    文件:SearchAddressActivity.java   
private void updateToNewLocation(Location location) {
    if (location != null){
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }

    //搜索当前城市
    String uri = "http://restapi.amap.com/v3/geocode/regeo?key="
            + KEY + "&location=" + longitude + "," + latitude +
            "&poitype=&radius=&extensions=base&batch=false&roadlevel=";
    JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(
            Request.Method.GET,
            uri,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        JSONObject jsonObject1 = new JSONObject(jsonObject.toString());
                        JSONObject jsonObject2 = jsonObject1.getJSONObject("regeocode");
                        JSONObject jsonObject3 = jsonObject2.getJSONObject("addressComponent");
                        address = jsonObject3.getString("province");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.d("------volleyError",volleyError.toString());
        }
    }
    ){
        @Override
        public Map<String, String> getHeaders() {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json; charset=UTF-8");
            return headers;
        }
    };
    VolleyUtil.getInstance(this).addRequest(jsonRequest);
}
项目:ktball    文件:LoginActivity.java   
public void login(){
    String url = Constants.KTHOST+"users/login";
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("account", user);
        jsonObject.put("password", password);
        jsonObject.put("authenticity_token", MD5.getToken(url));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Request.Method.POST,url, jsonObject,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    closeLoadingDialog();
                    LogUtils.e(response.toString());
                    //关闭动画
                    //解析从服务器上接收到的json数据
                    UserMsg userMsg = new Gson().fromJson(response.toString(),new TypeToken<UserMsg>(){}.getType());
                    KTApplication.setLocalUserInfo(response.toString());
                    //返回的是错误
                    if (userMsg.response.equals("error")){
                        showDialogToast("用户名密码错误");
                    } else if (userMsg.is_school_manager == 0){
                        showDialogToast("您不是校园俱乐部管理");
                    } else if (userMsg.is_judge == 0){//如果不是裁判
                        showDialogToast("没有裁判权限");
                    } else {
                        PreferenceManager.getDefaultSharedPreferences(LoginActivity.this)
                                .edit()
                                .putLong(PRE_CURRENT_USER_ID,userMsg.user_id)
                                .putLong(PRE_CURRENT_CLUB_ID,userMsg.club_id)
                                .putString(PRE_CURRENT_CLUB_NAME,userMsg.club_name)
                                .putInt(PRE_CURRENT_TAYPE,userMsg.school_role.role_type)
                                .commit();
                        //把俱乐部的id用意图传递过去
                        Intent intent = new Intent(LoginActivity.this,ClubDataActivity3.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            closeLoadingDialog();
            //关闭动画
            showDialogToast("网络错误");
        }
    })
    {
        @Override
        public Map<String, String> getHeaders() {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Accept", "application/json");
            headers.put("Content-Type", "application/json; charset=UTF-8");
            return headers;
        }
    };
    VolleyUtil.getInstance(getApplicationContext()).addRequest(jsonRequest);
}
项目:eManga    文件:CoverListener.java   
@Override
public void onErrorResponse(VolleyError error) {
    final CoverListener listenerReference = this;
    new AsyncTask<Void,Void,Void>(){

        @Override
        protected Void doInBackground(Void... voids) {
            if(mManga != null) {
                if (mUrlError == null) {
                    mUrlError = new HashSet<String>(3);
                }

                mUrlError.add(mManga.cover);

                if (mRetries > 0) {
                    Log.d(TAG, "Cover: " + mManga.cover + "\nAsk to: " + Internet.HOST + "manga/" + mManga._id + "/cover?" + Internet.arrayParams(mUrlError, "e"));
                    App.getInstance().addToRequestQueue(
                            new MangaRequest(
                                    JsonRequest.Method.GET,
                                    Internet.HOST + "manga/" + mManga._id + "/cover?" + Internet.arrayParams(mUrlError, "e"),
                                    new Response.Listener<Manga>() {
                                        @Override
                                        public void onResponse(Manga response) {
                                            Log.d(TAG, response.toString());
                                            if (response.cover != null) {
                                                Log.d(TAG, "New cover received: " + response.title + " " + response.cover);
                                                mManga.cover = response.cover;

                                                // Reload image
                                                ImageCacheManager.getInstance().getImage(mManga.cover, mImageView, listenerReference);

                                                // Update database
                                                DatabaseHelper dbs = OpenHelperManager.getHelper(
                                                        App.getInstance().getApplicationContext(),
                                                        DatabaseHelper.class);
                                                dbs.getMangaRunDao().update(mManga);
                                                OpenHelperManager.releaseHelper();
                                            } else {
                                                Log.d(TAG, "There aren't new covers for: " + mManga.title);
                                            }
                                        }
                                    },
                                    null
                            ),
                            "New Cover"
                    );
                    mRetries--;
                } else {
                    Log.d(TAG, "Reached maximum intents number for ask a new cover with: " + mManga.cover);
                }
            }
            return null;
        }

    }.execute();
}
项目:restafari    文件:RequestLoggingHelper.java   
/**
 * Gets Request response information.
 * 
 * @param request
 *            Request instance to retrieve information for logging
 * 
 * @param response
 *            Response instance to retrieve information for logging
 * 
 * @return Message describing response, including Method type, URL and JSON response
 * */
static String getRequestResponseText(JsonRequest<?> request, JSONObject response)
{
    StringBuilder msg = new StringBuilder();

    msg.append( methodToString( request.getMethod() ) ).append(" request successful ").append("\n");
    msg.append("URL: ").append(request.getUrl()).append("\n");
    msg.append("Response: ").append(response.toString()).append("\n");

    return msg.toString();
}
项目:restafari    文件:RequestLoggingHelper.java   
/**
 * Gets Request response information.
 *
 * @param request
 *            Request instance to retrieve information for logging
 *
 * @param response
 *            Response instance to retrieve information for logging
 *
 * @return Message describing response, including Method type, URL and JSON response
 * */
static String getRequestResponseText(JsonRequest<?> request, JSONArray response)
{
    StringBuilder msg = new StringBuilder();

    msg.append( methodToString( request.getMethod() ) ).append(" request successful ").append("\n");
    msg.append("URL: ").append(request.getUrl()).append("\n");
    msg.append("Response: ").append(response.toString()).append("\n");

    return msg.toString();
}