android 中使用volley进行网络通信一些小问题

android 中使用volley进行网络通信一些小问题,第1张

1.volley 不需要开启线程 文件上传和下载 这个就亮桥不知道了 volley主要针对的是图片 如配升果你想用文件培键老 还是用https://github.com/loopj/android-async-http这个 而且有例子

final Map<雹悉String, String>params = new HashMap<String, String>()

params.put("send_userId", String.valueOf(id))

params.put("send_email", address)

params.put("send_name", name)

params.put("receive_email", emails)

final Map<戚猛String, File>files = new HashMap<String, File>()

files.put("源仔乎uploadfile", file)

final String request = UploadUtil.post(requestURL, params, files)

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

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext())

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

new Response.Listener<String>() {

@Override

public void onResponse(String response) {

Log.d(TAG, "好孝response ->举衫 " + response)

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

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

}

}) {

@Override

protected Map<String, String>getParams() {

//在这里正袜腔设置需要post的参数

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

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

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

return params

}

}

requestQueue.add(stringRequest)

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

RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext())

Map<String, String>map = new HashMap<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) {

Log.d(TAG, "response ->" + response.toString())

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

Log.e(TAG, error.getMessage(), 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

//}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/12234845.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存