Android 安卓截击反应

Android 安卓截击反应,android,json,compiler-errors,android-volley,response,Android,Json,Compiler Errors,Android Volley,Response,我正在尝试创建一个简单的登录,如果成功,我将尝试将响应userid(uid)保存到共享首选项中。下面是my loginActivity.java的一部分,错误消息如下: 错误1 error: incompatible types: int cannot be converted to String JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, Config.LOGIN_URL, null, new Re

我正在尝试创建一个简单的登录,如果成功,我将尝试将响应userid(uid)保存到共享首选项中。下面是my loginActivity.java的一部分,错误消息如下:

错误1

error: incompatible types: int cannot be converted to String
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, Config.LOGIN_URL, null, new Response.Listener<JSONObject>() {
错误:不兼容的类型:int无法转换为字符串
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,Config.LOGIN\u URL,null,new Response.Listener(){
错误2

error: cannot find symbol
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,Config.LOGIN_URL, null, new Response.Listener<JSONObject>() {
                                   ^
错误:找不到符号
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,Config.LOGIN\u URL,null,new Response.Listener(){
^
符号:构造函数(int,String,,>)

LoginActivity.java

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONObject;

import com.android.volley.Request.Method;
import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    //Defining views
    private EditText editTextTCNO;
    private EditText editTextMobile;
    private AppCompatButton buttonLogin;

    //boolean variable to check user is logged in or not
    //initially it is false
    private boolean loggedIn = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        //Initializing views
        editTextTCNO = (EditText) findViewById(R.id.editTextTCNO);
        editTextMobile = (EditText) findViewById(R.id.editTextMobile);

        buttonLogin = (AppCompatButton) findViewById(R.id.loginButton);

        //Adding click listener
        buttonLogin.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //In onresume fetching value from sharedpreference
        SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);

        //Fetching the boolean value form sharedpreferences
        loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);

        //If we will get true
        if(loggedIn){
            //We will start the Profile Activity
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
        }
    }

    private void login() {
        //Getting values from edit texts
        final String tcno = editTextTCNO.getText().toString().trim();
        final String mobile = editTextMobile.getText().toString().trim();
        final String operation = "login";

        //Creating a string request
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, Config.LOGIN_URL, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String smsg = response.getString("msg");
                String pid = response.getString("uid");

                //If we are getting success from server
                if(smsg == Config.LOGIN_SUCCESS){
                    //Creating a shared preference
                    SharedPreferences sharedPreferences = LoginActivity.this.getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);

                    //Creating editor to store values to shared preferences
                    SharedPreferences.Editor editor = sharedPreferences.edit();

                    //Adding values to editor
                    editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, true);
                    editor.putString(Config.UID_SHARED_PREF, pid);

                    //Saving values to editor
                    editor.commit();

                    //Starting profile activity
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                }else{
                    //If the server response is not success
                    //Displaying an error message on toast
                    Toast.makeText(LoginActivity.this, "Invalid username or password", Toast.LENGTH_LONG).show();
                }
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                //Adding parameters to request
                params.put(Config.KEY_TCNO, tcno);
                params.put(Config.KEY_MOBILE, mobile);
                params.put(Config.KEY_OPERATION, operation);

                //returning parameter
                return params;
            }
        };

        //Adding the string request to the queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonObjReq);

    }

    @Override
    public void onClick(View v) {
        //Calling the login function
        login();
    }
}
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.support.v7.app.ActionBar;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.AppCompatButton;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.Toast;
导入org.json.JSONObject;
导入com.android.volley.Request.Method;
导入com.android.volley.AuthFailureError;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
导入java.util.HashMap;
导入java.util.Map;
公共类LoginActivity扩展了AppCompatActivity实现了View.OnClickListener{
//定义视图
私有编辑文本编辑文本编号;
私人编辑手机;
私有应用程序按钮登录;
//用于检查用户是否登录的布尔变量
//起初这是错误的
私有布尔loggedIn=false;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
ActionBar ActionBar=getSupportActionBar();
actionBar.hide();
//初始化视图
editTextTCNO=(EditText)findViewById(R.id.editTextTCNO);
editTextMobile=(EditText)findViewById(R.id.editTextMobile);
buttonLogin=(AppCompatButton)findViewById(R.id.loginButton);
//添加单击侦听器
buttonLogin.setOnClickListener(此);
}
@凌驾
受保护的void onResume(){
super.onResume();
//在onresume中从SharedReference获取值
SharedReferences SharedReferences=GetSharedReferences(Config.SHARED\u PREF\u NAME,Context.MODE\u PRIVATE);
//从SharedReferences获取布尔值
loggedIn=SharedReferences.getBoolean(Config.loggedIn\u SHARED\u PREF,false);
//如果我们能实现
if(loggedIn){
//我们将启动配置文件活动
意向意向=新意向(LoginActivity.this、MainActivity.class);
星触觉(意向);
}
}
私有void登录(){
//从编辑文本中获取值
最后一个字符串tcno=editTextTCNO.getText().toString().trim();
final String mobile=editTextMobile.getText().toString().trim();
最终字符串操作=“登录”;
//创建字符串请求
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.POST,Config.LOGIN\u URL,null,new Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
字符串smsg=response.getString(“msg”);
stringpid=response.getString(“uid”);
//如果我们从服务器获得成功
if(smsg==Config.LOGIN\u成功){
//创建共享首选项
SharedReferences SharedReferences=LoginActivity.this.getSharedReferences(Config.SHARED\u PREF\u NAME,Context.MODE\u PRIVATE);
//创建编辑器以将值存储到共享首选项
SharedReferences.Editor=SharedReferences.edit();
//向编辑器添加值
putBoolean(Config.LOGGEDIN\u SHARED\u PREF,true);
putString(Config.UID\u SHARED\u PREF,pid);
//将值保存到编辑器
commit();
//启动配置文件活动
意向意向=新意向(LoginActivity.this、MainActivity.class);
星触觉(意向);
}否则{
//如果服务器响应不成功
//在toast上显示错误消息
Toast.makeText(LoginActivity.this,“无效用户名或密码”,Toast.LENGTH_LONG.show();
}
}
}){
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
//向请求添加参数
参数put(Config.KEY\u TCNO,TCNO);
参数put(Config.KEY_MOBILE,MOBILE);
参数put(Config.KEY_操作,操作);
//返回参数
返回参数;
}
};
//将字符串请求添加到队列
RequestQueue RequestQueue=Volley.newRequestQueue(this);
add(jsonObjReq);
}
@凌驾
公共void onClick(视图v){
//调用登录函数
登录();
}
}

这是我提出截击请求的全功能方式,希望能有所帮助

private JSONObject LoginJson()
{
    JSONObject jsonBody = new JSONObject();
    try {
        jsonBody.put("Username", "testUser");
        jsonBody.put("Password", "123456");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonBody;
}
public boolean identificarce ()
{
    RequestFuture<JSONObject> future = RequestFuture.newFuture();
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.ServicioLogging, this.LoginJson(), future, future);

    QueuHolder.getInstance(Login.contexto).getRequestQueue().add(request);
    try {
        JSONObject response = future.get();
        try
        {
            String NombreCompleto = response.getString("NombreCompleto");
            int id = response.getInt("id");
            //save results in shared preferences
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return false;
    }
    return true;
}
private JSONObject LoginJson()
{
JSONObject jsonBody=新的JSONObject();
试一试{
put(“用户名”、“测试用户”);
jsonBody.put(“密码”,“123456”);
}捕获(JSONException e){
e、 printStackTrace();
}
返回jsonBody;
}
公共布尔标识()
{
RequestFuture=RequestFuture.newFuture();
JsonObjectRequest请求=新Js