短小故障使android动态

时间:2017-07-06 04:23:50

标签: android

我正在使用params发送一个json请求,故障是当我对用户名和密码进行硬编码时,它可以正常工作,当我设置变量时它没有? 在" ankur@gmail.com" &安培; " 123456"

    EditText username = (EditText)findViewById(R.id.edituser);
            EditText password = (EditText)findViewById(R.id.editpass);
            Button button = (Button)findViewById(R.id.signin);
            final JSONObject jsonObject = new JSONObject();
            String temp = '\u0022' +username.getText().toString().trim()+'\u0022';
            String temp1 ='\u0022'+password.getText().toString().trim()+'\u0022';

.........


final HashMap<String,String> params = new HashMap<String, String>();
    params.put("user_email","ankur@gmail.com");
    params.put("user_password","123456");
    Log.d("jsonobject", "jsonobjectResponse: "+params);

.........

    button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url,new JSONObject(params),
                            new Response.Listener<JSONObject>() {   .........

日志

07-06 12:23:32.365 4701-4701/com.production.vy.triqsquad D/jsonobject: jsonobjectResponse: {user_password=, user_email=}
07-06 12:23:56.046 4701-4701/com.production.vy.triqsquad D/aaa: onErrorResponse: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
07-06 12:24:04.765 4701-4701/com.production.vy.triqsquad D/aaa: onErrorResponse: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
07-06 12:24:07.584 4701-4701/com.production.vy.triqsquad D/aaa: onErrorResponse: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

为什么密码在用户名之前,当我在用户名上面写密码

2 个答案:

答案 0 :(得分:1)

试试这个

.........

// remove the '\u0022'
String temp = username.getText().toString().trim();
String temp1 =password.getText().toString().trim();

.........


final HashMap<String,String> params = new HashMap<String, String>();
params.put("user_email", temp);
params.put("user_password", temp1);
Log.d("jsonobject", "jsonobjectResponse: "+params);


我想的是你不必插入&#39; \ u0022&#39;你自己。 <{1}}会在字符串化时为你做。


你也在开发服务器吗?如果是这样,您还可以访问服务器以查看服务器接收的内容,以便更好地解决当前问题。


如果您也提交logcat / console错误,其他人将提供更好的答案。

答案 1 :(得分:0)

 EditText username = (EditText)findViewById(R.id.edituser);
            EditText password = (EditText)findViewById(R.id.editpass);
            Button button = (Button)findViewById(R.id.signin);
            final JSONObject jsonObject = new JSONObject();
            String temp = '\u0022' +username.getText().toString().trim()+'\u0022';
            String temp1 ='\u0022'+password.getText().toString().trim()+'\u0022';

.........


final HashMap<String,String> params = new HashMap<String, String>();


.........

    button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
params.put("user_email","ankur@gmail.com");
        params.put("user_password","123456");
        Log.d("jsonobject", "jsonobjectResponse: "+params);
                        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url,new JSONObject(params),
                                new Response.Listener<JSONObject>() {   .........

已经完成了