【问题标题】:Add Parameters to a Request向请求添加参数
【发布时间】:2015-08-22 02:54:54
【问题描述】:

我正在尝试向我的服务器发送一个POSTRequest,它返回一个JSONObject,但是它需要这种长字符串形式的参数:

client_id=client_id&client_secret=client_secret&grant_type=password&username=username&password=password

我一直在试图弄清楚如何做到这一点,但是与此类似的每个教程或答案都使用已弃用的 HTTPClient 等。有人可以指出我正确的方向吗?

【问题讨论】:

    标签: android http-post httprequest android-volley


    【解决方案1】:

    HttpClient 自 API 级别 22 起已被弃用

    请改用HttpURLConnection,例如here。您也可以阅读此post 了解更多信息。

    我建议使用Retrofit,因为它可以将JSON 请求管理到POJOs 更容易。

    【讨论】:

    • 就像我在问题中所说的那样,Android Studio(和其他来源)告诉我 HttpClient 和 HttpPost 已贬值。他们在撒谎吗?
    • @MatthewSteinhardt 抱歉,我读错了问题。检查编辑
    • 我的主要失败:P对不起。
    • 别担心,您修改后的回答就像一个魅力!谢谢。
    • 我希望你使用改造?
    【解决方案2】:

    您可以使用此代码:

     public JSONObject getJSONObject(String url, List<NameValuePair> parameters) {
    
        HttpPost httpPost = new HttpPost(url);
       /* httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("Accept", "application/json");*/
        httpPost.addHeader("api_key", mContext.getResources().getString(R.string.api_key));
        HttpResponse response = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(parameters));
            response = mHttpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String jsonResponse = EntityUtils.toString(entity);
            for (NameValuePair params : parameters) {
                Log.d("params:", params.toString());
            }
            Log.d("url:", url);
            Log.d("EntityUtils", jsonResponse);
            JSONObject jsonObject = new JSONObject(jsonResponse);
            return jsonObject;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    
    }
    

    并将此方法称为:

     public String setCricketInfo(String userId, String role, String battingStyle) {
        List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new BasicNameValuePair(mContext.getString(R.string.role_json), role));
        JSONObject jsonObject = this.getJSONObject(BASE_URL, parameters);
        try {
            return jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    

    现在能够将请求发送到远程并以 Json 的形式获得响应 享受你的代码:)

    【讨论】:

      【解决方案3】:

      试试这个

      List<NameValuePair> params = new ArrayList<>();
      params.add("client_id", client_id);
      params.add("client_secret", client_secret);
      params.add("grant_type", password);
      params.add("username", username);
      params.add("password", password);
      
      JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://server.com", URLEncodedUtils.format(params, "utf-8"), handler, handler);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-18
        • 2016-04-13
        • 2015-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        相关资源
        最近更新 更多