标签:
在onCreate中
1 package com.turtle920.volleydemo; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.TextView; 9 10 import com.android.volley.AuthFailureError; 11 import com.android.volley.Request; 12 import com.android.volley.RequestQueue; 13 import com.android.volley.Response; 14 import com.android.volley.VolleyError; 15 import com.android.volley.toolbox.StringRequest; 16 import com.android.volley.toolbox.Volley; 17 18 import java.util.HashMap; 19 import java.util.Map; 20 21 public class MainActivity extends AppCompatActivity { 22 23 static final private String URL = "http://192.168.8.6/insert.php"; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 30 RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext()); 31 32 StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, 33 new Response.Listener<String>() { 34 @Override 35 public void onResponse(String response) { 36 Log.d("TAG", response);//response为服务器返回的string 37 } 38 }, new Response.ErrorListener() { 39 @Override 40 public void onErrorResponse(VolleyError error) { 41 Log.e("TAG", error.getMessage(), error); 42 } 43 }){ 44 @Override 45 protected Map<String, String> getParams() throws AuthFailureError { 46 Map<String, String> map = new HashMap<String, String>(); 47 map.put("firstname", "杰伦"); 48 map.put("lastname", "htt"); 49 map.put("age", "18"); 50 return map; 51 } 52 }; 53 54 mQueue.add(stringRequest); 55 56 } 57 }
注意在manifest文件中加入Internet权限
1 <uses-permission android:name="android.permission.INTERNET" />
标签:
原文地址:http://www.cnblogs.com/turtle920/p/4903256.html