在通过所有要求后,会发送已注册帐户的用户名和密码。必须包含1个小写,1个大写,1个数字和1个特殊字符,但由于某种原因,getParams()方法不起作用..它表示void结果,但这是在volley中给出的确切代码。有什么问题?
package com.example.eid.authenticator;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
import com.android.volley.Response;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.VolleyError;
import java.lang.Object;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final TextView mTextView = (TextView) findViewById(R.id.text);
// Request a string response from the provided URL.
Button registerButton = findViewById(R.id.registerButton);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText username = (EditText)findViewById(R.id.usernameField);
EditText password = (EditText)findViewById(R.id.passwordField);
final String sUsername = username.getText().toString();
final String sPassword = password.getText().toString();
final RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
final String url = "10.0.2.2";
if(sUsername.matches("")){
Toast.makeText(getApplicationContext(),"Please enter a username",Toast.LENGTH_SHORT).show();
}else if(sPassword.matches("")){
Toast.makeText(getApplicationContext(),"Please enter a password",Toast.LENGTH_SHORT).show();
}else if(sPassword.matches("^([^0-9]*|[^A-Z]*|[^a-z]*|[a-zA-Z0-9]*)$")){
Toast.makeText(getApplicationContext(),"Password must contain letters and numbers and uppercase",Toast.LENGTH_SHORT).show();
}
else if(sPassword.length() < 6){
Toast.makeText(getApplicationContext(),"Password must be greater than 6 characters",Toast.LENGTH_SHORT).show();
}
// TODO Auto-generated method stub
else {
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
})
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user",sUsername);
params.put("pass",sPassword);
return params;
}
Intent i = new Intent(RegisterActivity.this, waitingPage.class);
startActivity(i);
}
}
});
}
}
这是错误的屏幕截图,它是我认为的语法,但我完全没有看到它。它看起来很好。
错误图片
答案 0 :(得分:0)
GET 方法永远不会调用getParams()
,如果您需要在 GET 中传递参数,则需要将其作为url params传递,getParams()
会在 POST 请求中调用,请参阅下面的
将此转换为发布请求
final StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
答案 1 :(得分:0)
首先,您使用的是GET
Request.Method.GET
应该是POST
喜欢
Request.Method.POST
您需要将getParams()
方法保留在{ };
内,如:
@Override
public void onErrorResponse (VolleyError error){
mTextView.setText("That didn't work!");
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("user", sUsername);
params.put("pass", sPassword);
return params;
}
};
答案 2 :(得分:0)
试试这个
您在getParams()之前
{
之后忘记了})
并且忘记了。{1}} getParams();
final StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Androidhive");
params.put("email", "abc@androidhive.info");
params.put("password", "password123");
return params;
}
};
网址应为http://10.0.2.2/folder_name/login.php
答案 3 :(得分:0)
将其更改为:
final StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
并将您的网址更改为:http://10.0.2.2/login.com
答案 4 :(得分:0)
试试这个:
private void getRideStatus() {
StringRequest req = new StringRequest(Request.Method.POST, Global.BASE_URL + Global.GET_RIDE_STATUS, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("AA", response);
try {
JSONObject obj = new JSONObject(response);
if (obj.getString("code").equals("1")) {
alertDialog(API.rMessage(obj));
loader.dismiss();
waitTimer.cancel();
}
else if (obj.getString("code").equals("2")) {
loader.show();
// dialog.setMessage(obj.getString("message"));
// alertDialog(API.rMessage(obj));
// loader.dismiss();
} else if (obj.getString("code").equals("3")) {
loader.dismiss();
alertDialog(API.rMessage(obj));
// setAlert("Normal");
waitTimer.cancel();
} else if (obj.getString("code").equals("4")) {
loader.dismiss();
waitTimer.cancel();
setAlert("Normal");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
//Pass the parameters to according to the API.
Map<String, String> params = new HashMap<String, String>();
Gson gson = new Gson();
String json = Utils.getUserPreferences(HomeActivity.this, Global.RESPONSE_COUPON);
CustomerCoupon obj = gson.fromJson(json, CustomerCoupon.class);
params.put("user_token", Utils.getUser(HomeActivity.this).getUser_token());
params.put("order_id", "");
params.put("customer_latitude", String.valueOf(sourceLL.latitude));
params.put("customer_longitude", "" + sourceLL.longitude);
params.put("continue","");
Log.e("AA", params.toString());
return params;
}
};
AppController.getInstance().addToRequestQueue(req);
}