作者:浪漫满屋19860902692 | 来源:互联网 | 2023-01-31 13:22
我是Java新手.我想将post json数据发送到webserver.我的排球发布如下.
public void postData(String url,JSONObject data,final VolleyCallback mResultCallback){
RequestQueue requstQueue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsOnobj= new JsonObjectRequest(Request.Method.POST, url,null,
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
if(mResultCallback != null){
mResultCallback.notifySuccess(response);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(mResultCallback != null){
mResultCallback.notifyError(error);
}
}
}
){
//here I want to post data to sever
};
requstQueue.add(jsonobj);
}
这是我的MainActivity代码
JSONObject data = null;
try {
String datas = "{'email': email,'password': password}";
data = new JSONObject(datas);
}catch (JSONException e){
e.printStackTrace();
}
String url = "http://example.com";
我想将JSON数据发布到PostData方法.如何将此json数据发布到我的服务器?
1> Ranjan..:
public void postData(String url,Hashmap data,final VolleyCallback mResultCallback){
RequestQueue requstQueue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsOnobj= new JsonObjectRequest(Request.Method.POST, url,new JSONObject(data),
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
if(mResultCallback != null){
mResultCallback.notifySuccess(response);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(mResultCallback != null){
mResultCallback.notifyError(error);
}
}
}
){
//here I want to post data to sever
};
requstQueue.add(jsonobj);
}
现在,来自你的mainActiviy课程
Hashmap data = new HashMap();
data.put("email","email");
data.put("password","password");
String url = "http://example.com";
//now you can just call the method, I have rectified your string to hashmap,
postData(url,data,new mResultCallb..... //rest of your code