【问题标题】:Android Volley LibraryAndroid Volley 库
【发布时间】:2017-01-30 12:04:37
【问题描述】:

我想知道如果登录请求 url 是,如何发送 volley 登录请求

username:password http://‎login_url

以这种格式。如果有示例代码可用,那将是一个很大的帮助。 目的是将登录请求发送到基于 django 框架的网站。

提前致谢

【问题讨论】:

  • 你能告诉我你做了什么吗???
  • curl -H "Content-Type:application/json" -X POST -d {"email":"someone@example.com","first_name":"FName","last_name": "Lname","password":"pass123"}' 192.xxx.xxx.xxx:1111/register_user 这就是我应该如何将值传递给服务器.. 任何人都可以帮助我.. 这怎么可能使用 volley?

标签: android api django-models android-volley


【解决方案1】:

首先创建一个字符串请求->>

 StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                    }
                });

那么,你应该重写 getParams() 方法!

@Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put(KEY_USERNAME,username);
                params.put(KEY_PASSWORD,password);
                params.put(KEY_EMAIL, email);
                return params;

将请求添加到请求队列!

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

三个简单的步骤:)

有任何疑问欢迎提问

【讨论】:

    【解决方案2】:

    试试这个

    // Tag used to cancel the request
    String tag_json_obj = "json_obj_req";
    
    String url = "http:‎login_url";
    
    ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();     
    
            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
                    url, null,
                    new Response.Listener<JSONObject>() {
    
                        @Override
                        public void onResponse(JSONObject response) {
                //your response
                            Log.d(TAG, response.toString());
                            pDialog.hide();
                        }
                    }, new Response.ErrorListener() {
    
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d(TAG, "Error: " + error.getMessage());
                            pDialog.hide();
                        }
                    }) {
    
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("name", "username");
                    params.put("email", "abc@dffg.info");
                    params.put("password", "password123");
    
                    return params;
                }
    
            };
    
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 2014-04-29
      • 1970-01-01
      相关资源
      最近更新 更多