好的,所以这里是用于发送带参数的截击请求的android代码。
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", "xyz");
params.put("email", "abc@xyz.info");
params.put("password", "password123");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
我需要帮助使用 java servlet 在服务器端读取这些值。我试过到处寻找答案,但我在网上找不到任何东西。 另外我不确定android代码是完美的。请指出错误。
答案 0 :(得分:3)
好的,你必须这样做。
private void ServerRequest() {
String emailId = email_id.getText().toString();
String pass = password.getText().toString();
Map<String, String> params = new HashMap<String, String>();
params.put("email", emailId);
params.put("password", pass);
CustomRequest jsObjRequest = new CustomRequest(Request.Method.POST, Const.url_login, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
stopAnim();
Log.d("Response: ", response.toString());
String s = null;
s = response.getString("info");
if (s.equals("Login successful")) {
session.createLoginSession(email_id.getText().toString(), password.getText().toString());
Intent login = new Intent(getApplicationContext(), CustomViewIconTextTabsActivity.class);
startActivity(login);
finish();
}
else {
//handle the else condition here
}
} catch (Exception e) {
// TODO Auto-generated catch block
Crouton.makeText(Login_Activity.this, "Error Occured during login!", Style.ALERT,
(ViewGroup)findViewById(R.id.croutonview))
.show();
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError response) {
Log.d("Response: ", response.toString());
if (response instanceof TimeoutError || response instanceof NoConnectionError) {
Crouton.makeText(Login_Activity.this, "Connection Timeout", Style.ALERT,
(ViewGroup)findViewById(R.id.croutonview))
.show();
} else if (response instanceof AuthFailureError) {
//TODO
} else if (response instanceof ServerError) {
//TODO
Toast.makeText(getApplicationContext(),"Server Error",
Toast.LENGTH_LONG).show();
stopAnim();
} else if (response instanceof NetworkError) {
//TODO
} else if (response instanceof ParseError) {
//TODO
}
}
});
int socketTimeout = 5000;//5 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsObjRequest.setRetryPolicy(policy);
AppController.getInstance().addToRequestQueue(jsObjRequest);
// Cancelling request
// ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);
}
以上代码是使用排球进行自定义请求。
服务器端代码如下。
Map m=request.getParameterMap();
Set s = m.entrySet();
Iterator it = s.iterator();
int index=0;
String email=null,password=null;
while(it.hasNext()){
Map.Entry<String,String[]> entry = (Map.Entry<String,String[]>)it.next();
String key = entry.getKey();
String[] value = entry.getValue();
System.out.println("Key is "+key);
if(value.length>1){
for (int i = 0; i < value.length; i++) {
System.out.println( value[i].toString());
switch(key)
{
case "email":
email=value[0].toString();
break;
case "password":
password=value[0].toString();
break;
}
}
}else
{
System.out.println("Value is "+value[0].toString());
switch(key)
{
case "email":
email=value[0].toString();
break;
case "password":
password=value[0].toString();
break;
}
}
}
希望这会对你有所帮助.. :)