Android截击发送参数始终为空
我是android新手。现在我正在做一个应用程序。对于这个应用程序,我需要将数据发送到服务器。现在我使用的是Volley post方法。但是当我使用Volley将数据发送到服务器时,参数总是显示为null。这里我附加了代码,请检查。这里我使用的是片段 代码部分Android截击发送参数始终为空,android,post,android-volley,Android,Post,Android Volley,我是android新手。现在我正在做一个应用程序。对于这个应用程序,我需要将数据发送到服务器。现在我使用的是Volley post方法。但是当我使用Volley将数据发送到服务器时,参数总是显示为null。这里我附加了代码,请检查。这里我使用的是片段 代码部分 String url = "http://192.168.1.182:8084/name/registration.jsp"; final ProgressDialog pDialog = new ProgressDialog(
String url = "http://192.168.1.182:8084/name/registration.jsp";
final ProgressDialog pDialog = new ProgressDialog(this.getActivity());
pDialog.setMessage("Loading...");
pDialog.show();
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
// pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
//pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Ajay K K");
params.put("mailid", "ajaykk50@gmail.com");
params.put("phone", "8086327023");
params.put("place", "Calicut");
params.put("longitude","44444.3333");
params.put("latitude","666666.3333");
params.put("wheel", "1");
params.put("type", "owner");
return params;
}
};
// Adding request to request queue
rq.add(jsonObjReq);
stringurl=”http://192.168.1.182:8084/name/registration.jsp";
final ProgressDialog pDialog=新建ProgressDialog(this.getActivity());
设置消息(“加载…”);
pDialog.show();
RequestQueue rq=Volley.newRequestQueue(getActivity().getApplicationContext());
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,
url,空,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
//pDialog.hide();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
//pDialog.hide();
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
参数put(“名称”、“Ajay K”);
参数put(“mailid”ajaykk50@gmail.com");
参数put(“电话”,“8086327023”);
参数放置(“放置”、“校准切割”);
参数put(“经度”,“44444.3333”);
参数put(“纬度”,“666666.3333”);
参数put(“车轮”、“1”);
参数put(“类型”、“所有者”);
返回参数;
}
};
//将请求添加到请求队列
rq.add(jsonObjReq);
不要覆盖getParams()
JsonObjectRequest
使用构造函数中的第三个参数来获取post参数。下面是包含在volley代码中的文档
/**
* Creates a new request.
* @param method the HTTP method to use
* @param url URL to fetch the JSON from
* @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and
* indicates no parameters will be posted along with request.
* @param listener Listener to receive the JSON response
* @param errorListener Error listener, or null to ignore errors.
*/
public JsonObjectRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener) {
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener,
errorListener);
}
/**
*创建一个新请求。
*@param method要使用的HTTP方法
*@param url从中获取JSON
*@param jsonRequest A{@link JSONObject}与请求一起发布。允许Null,并且
*指示不会随请求一起发布任何参数。
*@param listener用于接收JSON响应
*@param errorListener Error listener,或null忽略错误。
*/
公共JSONObject请求(int方法、字符串url、JSONObject jsonRequest、,
监听器监听器,错误监听器(错误监听器){
super(方法,url,(jsonRequest==null)?null:jsonRequest.toString(),侦听器,
错误监听器);
}
像这样使用
String url = "http://192.168.1.182:8084/name/registration.jsp";
final ProgressDialog pDialog = new ProgressDialog(this.getActivity());
pDialog.setMessage("Loading...");
pDialog.show();
RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
JSONObject params = new JSONObject();
try {
params.put("name", "Ajay K K");
params.put("mailid", "ajaykk50@gmail.com");
params.put("phone", "8086327023");
params.put("place", "Calicut");
params.put("longitude","44444.3333");
params.put("latitude","666666.3333");
params.put("wheel", "1");
params.put("type", "owner");
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, params, //Not null.
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
// pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
//pDialog.hide();
}
});
// Adding request to request queue
rq.add(jsonObjReq);
stringurl=”http://192.168.1.182:8084/name/registration.jsp";
final ProgressDialog pDialog=新建ProgressDialog(this.getActivity());
设置消息(“加载…”);
pDialog.show();
RequestQueue rq=Volley.newRequestQueue(getActivity().getApplicationContext());
JSONObject参数=新的JSONObject();
试一试{
参数put(“名称”、“Ajay K”);
参数put(“mailid”ajaykk50@gmail.com");
参数put(“电话”,“8086327023”);
参数放置(“放置”、“校准切割”);
参数put(“经度”,“44444.3333”);
参数put(“纬度”,“666666.3333”);
参数put(“车轮”、“1”);
参数put(“类型”、“所有者”);
}捕获(JSONException e){
e、 printStackTrace();
}
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,
url,参数,//不为空。
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
//pDialog.hide();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
//pDialog.hide();
}
});
//将请求添加到请求队列
rq.add(jsonObjReq);
公共类CustomJsonRequest扩展请求{
映射参数;
私人回应。倾听者;
公共CustomJsonRequest(int-requestMethod、字符串url、映射参数、,
Response.Listener responseListener,Response.ErrorListener ErrorListener){
super(requestMethod、url、errorListener);
this.params=params;
this.listener=responseListener;
}
@凌驾
受保护的void deliverResponse(对象响应){
onResponse(response);
}
@凌驾
公共映射getParams()引发AuthFailureError{
返回参数;
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
试一试{
String jsonString=新字符串(response.data,HttpHeaderParser.parseCharset(response.headers));
返回Response.success(新的JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(响应));
}捕获(不支持的编码异常e){
返回Response.error(新的ParseError(e));
}捕获(JSONException je){
返回Response.error(newparseerror(je));
}
}
}
导入com.android.volley.NetworkResponse;
导入com.android.volley.ParseError;
导入com.android.volley.Request;
导入com.android.volley.Response;
导入com.android.volley.Response.ErrorListener;
导入com.android.volley.Response.Listener;
导入com.android.volley.toolbox.HttpHeaderParser;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.UnsupportedEncodingException;
导入java.util.Map;
公共类CustomRequest扩展了请求{
私人倾听者;
私有映射参数;
公共CustomRequest(字符串url、映射参数、,
侦听器reposeListener,ErrorListener ErrorListener){
super(Method.GET、url、errorListener);
this.listener=reponlistener;
this.params=params;
}
公共CustomRequest(int方法、字符串url、映射参数、,
侦听器reposeListener,ErrorListener ErrorListener){
super(方法、url、errorListener);
this.listener=reponlistener;
this.params=params;
}
受保护的映射getParams()
抛出com.android.volley.AuthFailureError{
返回
public class CustomJsonRequest extends Request {
Map<String, String> params;
private Response.Listener listener;
public CustomJsonRequest(int requestMethod, String url, Map<String, String> params,
Response.Listener responseListener, Response.ErrorListener errorListener) {
super(requestMethod, url, errorListener);
this.params = params;
this.listener = responseListener;
}
@Override
protected void deliverResponse(Object response) {
listener.onResponse(response);
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
return params;
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.util.Map;
public class CustomRequest extends Request<JSONObject> {
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
}
;
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
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) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
CustomRequest jsObjRequest = new CustomRequest(Request.Method.POST, LOGIN_URL, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("RESPONSE", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("RESPONSE ERROR", "Error: " + error.getMessage());
}
});
requestQueue.add(jsObjRequest);
Map<String, String> params = new HashMap<String, String>();
params.put("email", "yourEmail");
params.put("password", "yourPassword");
import android.graphics.Bitmap;
import android.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class VolleySingleton {
private static VolleySingleton sInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader imageLoader;
private VolleySingleton() {
mRequestQueue = Volley.newRequestQueue(RetriveMyApplicationContext.getAppContext());
imageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private LruCache<String, Bitmap> cache = new LruCache<>((int) (Runtime.getRuntime().maxMemory() / 1024 / 8));
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static VolleySingleton getInstance() {
if (sInstance == null) {
sInstance = new VolleySingleton();
}
return sInstance;
}
public RequestQueue getRequestQueue() {
return mRequestQueue;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
public class RetriveMyApplicationContext extends Application {
//Don't forget to mention RetriveMyApplicationContext Class in Manifests File otherwise it will throw NullPointer Exception
// <application
// android:name=".volley.RetriveMyApplicationContext"
private static RetriveMyApplicationContext mRetriveMyApplicationContext;
@Override
public void onCreate() {
super.onCreate();
mRetriveMyApplicationContext = this;
}
public static RetriveMyApplicationContext getInstance() {
return mRetriveMyApplicationContext;
}
public static Context getAppContext() {
return mRetriveMyApplicationContext.getApplicationContext();
}
}