高级部分 Volley框架的Post请求的使用

1.添加Volley框架

2.添加布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <Button
        android:id="@+id/str"
        android:onClick="clickPost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Str_Post" />
    
    <Button
        android:id="@+id/obj"
        android:onClick="clickPost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Obj_Post" />


</LinearLayout>


3.逻辑代码:

package com.qianfeng.volley_post;


import java.util.HashMap;
import java.util.Map;


import org.json.JSONException;
import org.json.JSONObject;


import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
//服务器连接
private String url ="http://10.0.2.2:8080/Volley_Post_GDay01/VolleyPostServer";
//任务队列
private RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue=Volley.newRequestQueue(getApplicationContext());
}
public void clickPost(View v){
int id=v.getId();
switch (id) {
case R.id.str://使用StringRequest实现Post请求
StringRequest stringRequest=new StringRequest(Method.POST, url, new Listener<String>() {


@Override
public void onResponse(String arg0) {
// TODO Auto-generated method stub
//提交成功
Toast.makeText(MainActivity.this,arg0,0).show();
}
}, new ErrorListener() {


@Override
public void onErrorResponse(VolleyError error) {
//提交失败
}
}){
//当把StringRequest的请求方式指定为post之后,Volley框架会调用StringRequest的getParams方法获得传递给服务器的参数
//所以重写该方法返回传递给服务器的参数
@Override
protected Map<String, String> getParams()
throws AuthFailureError {
HashMap<String, String> map = new HashMap<String, String>();
map.put("NAME", "abc");
map.put("PWD", "123");
return map;
}
};
queue.add(stringRequest);
break;
case R.id.obj:
//创建JSONObject封装传递给服务器端的参数
String json = "{\"NAME\":\"abc\",\"PWD\":\"123\"}";
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//创建发起Post请求的JsonObjectRequest对象
JsonObjectRequest objectRequest=new JsonObjectRequest(Method.POST, url, jsonObj, new Listener<JSONObject>() {


@Override
public void onResponse(JSONObject arg0) {
// TODO Auto-generated method stub
//提交成功
String result = arg0.toString();
Toast.makeText(MainActivity.this, result, 0).show();
}
}, new ErrorListener() {


@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
//提交失败

}
});
queue.add(objectRequest);
break;
default:
break;
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值