• Volley的post使用


    直接看代码,注意在manifest中加入Internet权限

    1  <uses-permission android:name="android.permission.INTERNET" />
    package com.turtle920.volleydemo;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.android.volley.AuthFailureError;
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.StringRequest;
    import com.android.volley.toolbox.Volley;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class MainActivity extends AppCompatActivity {
    
        static final private String URL = "http://192.168.8.6/insert.php";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
    
            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Log.d("TAG", response);//response为服务器返回的string
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("TAG", error.getMessage(), error);
                        }
            }){
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("firstname", "杰伦");
                    map.put("lastname", "htt");
                    map.put("age", "18");
                    return map;
                }
            };
    
            mQueue.add(stringRequest);
    
        }
    }

    Volley是个异步线程,需要在onResponse下完成要干的事,即使代码很丑

  • 相关阅读:
    剑指 Offer 30. 包含min函数的栈
    剑指 Offer 35. 复杂链表的复制
    剑指 Offer 18. 删除链表的节点
    剑指 Offer 24. 反转链表
    Jmeter
    Jmeter
    Linux搭建Jenkins
    [基金项目] 青年基金写作的历程与经验(2)
    [基金项目] 青年基金写作的历程与经验(1)
    浅析mysql存储过程
  • 原文地址:https://www.cnblogs.com/turtle920/p/4903256.html
Copyright © 2020-2023  润新知