2017-02-21 183 views
0

我想创建一个简单的登录,如果成功,我试图将响应用户ID(uid)保存到共享首选项中。下面是我loginActivity.java的部分和错误消息:Android Volley响应

错误1

error: incompatible types: int cannot be converted to String 
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, Config.LOGIN_URL, null, new Response.Listener<JSONObject>() { 

错误2

error: cannot find symbol 
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,Config.LOGIN_URL, null, new Response.Listener<JSONObject>() { 
           ^

符号:构造函数(INT,字符串,, >)

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(); 
    } 
} 
+0

我认为你输入了错误的'Method.POST' –

+0

我编辑的LoginActivity.java展现进口,这一个是错的? @ cricket_007 – adams

+0

也许不会...很难说出错误是指向什么。 Config.LOGIN_URL是一个整数吗? –

回答

1

这是我做出凌空要求全功能的方式,希望它有助于

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; 
} 
+0

这是否编译? JsonObjectRequest请求; =新的JsonObjectRequest' ...随机分号? –

+0

不,它没有。 @ cricket_007 – adams

+0

jajajaja我忘了,以前是这样的: JsonObjectRequest请求; request = new JsonObjectRequest(Request.Method.POST,URL.ServicioLogging,this.LoginJson(),future,future); 我测试,和它的作品,我定格在我的编辑:P 我们对此深感抱歉 – karique