码迷,mamicode.com
首页 > 其他好文 > 详细

Volley之post传输

时间:2015-08-15 11:51:51      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

1.客户端以普通的post方式进行提交,服务端返回字符串

RequestQueue requestQueue =Volley.newRequestQueue(this);

StringRequest stringRequest=new StringRequest(Request.Method.POST,httpurl,

new Response.Listener<String>(){

@Override

public void onResponse(String response){

System.out.println("response="+response);

//已经获取了数据

}

},new Response.ErrorListener(){

@Override

public void onErrorResponse(VolletError error){

 System.out.println("sorry,Error");

}

}){

@Override

protected Map<String,String> getParams(){

Map<String,String> map=new HashMap<String,String>();

map.put("name","value");

return params;

}};

requestQueue.add(stringRequest);

2.客户端以json串的post请求方式进行提交,服务端返回json串

RequestQueue requestQueue =Volley.newRequestQueue(this);

Map<String,String> map=new HashMao<String,String>();

map.put("name1","value1");

map.put("name2","value2");

JSONObject jsonObject=new JSONObject(params);

JsonRequest<JSONObject> jsonRequest=new JsonObjectRequest(Method.POST,httpurl,jsonObject,

     new Response.Listener<JSONObject>(){

@Override

public void onResponse(JSONObject response){

System.out.println("response="+response.toString());

}

},new Response.ErrorListener(){

@Override

public void onErrorResponse(VooleyError error){

System.out.println("sorry,Error");

}

}){

//注意此处override的getParams()方法,在此处设置post需要提交的参数根本不起作用
    //必须象上面那样,构成JSONObject当做实参传入JsonObjectRequest对象里
    //所以这个方法在此处是不需要的
//    @Override
//    protected Map<String, String> getParams() {                
//          Map<String, String> map = new HashMap<String, String>();  
//            map.put("name1", "value1");  
//            map.put("name2", "value2");  
                 
//        return params;
//    }
@Override
public Map<String,String> getHeaders(){
HashMap<String,String> headers=new HashMap<String,String>();
headers.put("Accept","application/json");
headers.put("Content-type","application/json;chaset=UTF-8");
return headers;
}

};

requestQueue.add(jsonRequest);

 

3.客户端以普通的post方式进行提交,服务端返回json串

首先在Activity类里,集成Request实现一个NormalPostRequest类

priavte class NormalPostRequest(String url,Listener<JSONObject> listener,

ErorListener errorListener,Map<String,String> map){

super(Request.Mthod.POST,url,errorListener);

mListener=listener;

mMap=map;

}

//mMap是已经按照前面的方式,设置了参数的实例

@Override

protected Map<String,String> getParams() throws AuthFailureError{

return mMap;

}

@Override

protected Response<JSONObject> parseNetworkResponse(NewworkResponse response){

try{

String jsonString=new String(response.data,HttpHeaderParse.parseCharset(response.headers));

return Response.success(ne JSONObject(josnString),HttpHeaderParser.parseCacheHeaders(response));

}catch(UnsupportedEncodingException e){

return Response.error(new ParseError(e));

}catch(JSONException je){

return Response.error(new ParseError(je));

}

}@Override

protected void deliverResponse(JSONObject response){

mListener.onResponse(response);

}

}

接下来的调用方式和前面差不多,生成一个Request实例,加入列队中即可

RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContexe());

Request<JSONObject> request=new NormalPostRequest(httpurl,

new Response.Listener<JSONObject>(){

@Override

public void onResponse(JSONObject  response){

System.out.println("response="+response.toString());

}

},new Response.ErrorListener(){

@Override

public void onResponse(VolleyError error){

Log.e("TAG",error.getMessage(),error);

}

},params);

requestQueue.add(request);

 

Volley之post传输

标签:

原文地址:http://www.cnblogs.com/chhom/p/4732041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!