Volley的GET和Post方法

 

 

 1 // Volley 的 Get 和 Post 请求的基本用法
 2 public class MainActivity extends ActionBarActivity {
 3 
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_main);
 8         // Volley_Get();
 9         Volley_Post();
10 
11     }
12 
13     // --------------------Post()方式请求网络------------------------------------
14     private void Volley_Post() {
15         String url = "http://apis.juhe.cn/mobile/get?";
16         StringRequest request = new StringRequest(Method.POST, url,
17                 new Listener<String>() {
18 
19                     @Override
20                     public void onResponse(String response) {
21                         // TODO Auto-generated method stub
22                         Toast.makeText(MainActivity.this, response, 1).show();
23                     }
24                 }, new Response.ErrorListener() {
25 
26                     @Override
27                     public void onErrorResponse(VolleyError error) {
28                         // TODO Auto-generated method stub
29                         Toast.makeText(MainActivity.this, "请求失败", 0).show();
30                     }
31                 }) {
32 
33             protected java.util.Map<String, String> getParams()
34                     throws com.android.volley.AuthFailureError {
35                 HashMap<String, String> map = new HashMap<String, String>();
36                 map.put("phone", "13883704133");
37                 map.put("key", "335adcc4e891ba4e4be6d7534fd54c5d");
38 
39                 return map;
40             }
41         };
42         request.setTag("abcPost");
43         MyAppplication.getHttpQueue().add(request);
44     }
45 
46     // --------------------Get()方式请求网络----------------------------------------------------
47     private void Volley_Get() {
48         // TODO Auto-generated method stub
49         String url = "http://apis.juhe.cn/mobile/get?phone=13666666666&key=335adcc4e891ba4e4be6d7534fd54c5d";
50         StringRequest request = new StringRequest(Method.GET, url,
51                 new Listener<String>() {
52 
53                     @Override
54                     public void onResponse(String response) {
55                         // TODO Auto-generated method stub
56                         Toast.makeText(MainActivity.this, response, 1).show();
57                     }
58                 }, new Response.ErrorListener() {
59 
60                     @Override
61                     public void onErrorResponse(VolleyError error) {
62                         // TODO Auto-generated method stub
63                         Toast.makeText(MainActivity.this, "请求失败", 0).show();
64                     }
65 
66                 });
67         request.setTag("abcGet");
68         MyAppplication.getHttpQueue().add(request);
69 
70     }
71     @Override
72     protected void onStop() {
73         // TODO Auto-generated method stub
74         super.onStop();
75         MyAppplication.getHttpQueue().cancelAll("abcPost");
76         MyAppplication.getHttpQueue().cancelAll("abcGet");
77     }
78 
79 }

 

转载于:https://www.cnblogs.com/my334420/p/6979124.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 中进行 JSON 的 POST 和 GET 请求,可以使用 Android 所提供的 HttpUrlConnection 类或者 Volley 框架,以下分别介绍两种方法: 1. 使用 HttpUrlConnection 进行 POST 和 GET 请求: - POST 请求: ```java URL url = new URL("http://example.com/api"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); conn.setRequestProperty("Accept", "application/json"); conn.setDoOutput(true); conn.setDoInput(true); JSONObject jsonParam = new JSONObject(); jsonParam.put("param1", "value1"); jsonParam.put("param2", "value2"); DataOutputStream os = new DataOutputStream(conn.getOutputStream()); os.writeBytes(jsonParam.toString()); os.flush(); os.close(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // Handle response } else { // Error handling } ``` - GET 请求: ```java URL url = new URL("http://example.com/api?param1=value1&param2=value2"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); conn.setRequestProperty("Accept", "application/json"); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // Handle response } else { // Error handling } ``` 2. 使用 Volley 进行 POST 和 GET 请求: - POST 请求: ```java String url = "http://example.com/api"; JSONObject jsonParam = new JSONObject(); jsonParam.put("param1", "value1"); jsonParam.put("param2", "value2"); JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonParam, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // Handle response } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // Error handling } }); request.setRetryPolicy(new DefaultRetryPolicy( 5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); RequestQueue queue = Volley.newRequestQueue(context); queue.add(request); ``` - GET 请求: ```java String url = "http://example.com/api?param1=value1&param2=value2"; JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // Handle response } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // Error handling } }); request.setRetryPolicy(new DefaultRetryPolicy( 5000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); RequestQueue queue = Volley.newRequestQueue(context); queue.add(request); ``` 需要注意的是,以上代码仅为示例,具体实现需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值