Android 如何通过截取将JSON对象发送到服务器
我正在使用Volley库从服务器获取JSON数据,但现在,我需要向服务器发送JSON。但我不知道如何发回JSONArray或JSONObject 我使用下一个示例检索数据Android 如何通过截取将JSON对象发送到服务器,android,json,send,android-volley,Android,Json,Send,Android Volley,我正在使用Volley库从服务器获取JSON数据,但现在,我需要向服务器发送JSON。但我不知道如何发回JSONArray或JSONObject 我使用下一个示例检索数据 JsonArrayRequest program_from_event = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray respo
JsonArrayRequest program_from_event = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
if(response.length() > 0){
for(int i = 0; i < response.length(); i++){
try {
JSONObject input = response.getJSONObject(i);
String name = input.getString("name");
String work_area = input.getString("work_area");
String email = input.getString("email");
String phone = input.getString("phone");
String avatar = input.getString("avatar");
String description = input.getString("description");
int id = input.getInt("id");
...
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse nr = error.networkResponse;
if(nr != null && nr.statusCode == HttpStatus.SC_UNAUTHORIZED){
Log.i("ERROR", "ERROR");
}
}
});
volley_queue.add(program_from_event);
JsonArrayRequest程序\u from_event=newjsonarrayrequest(URL,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
if(response.length()>0){
对于(int i=0;i
}
如何使用send方法?谢谢。受保护的void callWebService(最终用户){
protected void callWebService(final User user) {
pd = ProgressDialog.show(activity, "Please Wait...", "Please Wait...");
RequestQueue queue = Volley.newRequestQueue(activity);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.POST,url,
createUserMapperObejct(user),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
pd.dismiss();
Log.v("reponse", "" + response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(jsObjRequest);
}
public static JSONObject createUserMapperObejct(User user)
{
JSONObject request=new JSONObject();
try {
request.put(StringConstant.USERNAME, user.getUserName());
request.put(StringConstant.PASSWORD, user.getPassword());
request.put(StringConstant.CONFIRMPASSWORD, user.getPassword());
request.put(StringConstant.EMAILID, user.getEmailId());
request.put(StringConstant.CONTACTNO, user.getMobileNo());
} catch (JSONException e) {
e.printStackTrace();
}
return request;
}
pd=ProgressDialog.show(活动“请等待…”,请等待…”);
RequestQueue=Volley.newRequestQueue(活动);
JsonObjectRequest jsObjRequest=新的JsonObjectRequest(
Request.Method.POST、url、,
createUserMapperObejct(用户),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
pd.解散();
Log.v(“响应”、“响应”+);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
add(jsObjRequest);
}
公共静态JSONObject createUserMapperObejct(用户)
{
JSONObject请求=新建JSONObject();
试一试{
request.put(StringConstant.USERNAME,user.getUserName());
request.put(StringConstant.PASSWORD,user.getPassword());
request.put(StringConstant.CONFIRMPASSWORD,user.getPassword());
request.put(StringConstant.EMAILID,user.getEmailId());
request.put(StringConstant.CONTACTNO,user.getMobileNo());
}捕获(JSONException e){
e、 printStackTrace();
}
返回请求;
}
检查:谢谢,我会测试它。效果很好。谢谢