我正在使用 Volley 在 Android 设备和网络服务器之间传输数据。
我发现有关将数据列表发送到服务器的问题。
例如,我的类将生成如下数据集:
{
"1": {
"1_aID": "5",
"2_aID": "5",
"3_aID": "5",
"4_aID": "5"
},
"2": {
"1_bID": "3",
"2_bID": "3",
"3_bID": "3"
},
"3": {
"1_cID": "4"
}
}
我如何将这些数据发送到服务器?
我找到了一些将数据发布到服务器的教程。它必须使用 hashmap。
有更好的解决方案来处理这种情况吗?
- 发出如下的齐射请求,其方法如下
POST/GET
,
url
, response & error
听众。以及发送您的 json 覆盖getBody()
方法,其中传递要发送的 json。
- Make a
RequestQueue
并将请求添加到其中。你可以通过以下方式开始
呼叫start()
尝试这个 :
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// your response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
}
}){
@Override
public byte[] getBody() throws AuthFailureError {
String your_string_json = ; // put your json
return your_string_json.getBytes();
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
requestQueue.start();
欲了解更多信息,请参阅 this http://developer.android.com/training/volley/simple.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)