我正在创建一个包含登录表单的应用程序,我正在使用单例类,jsonobject请求和volley库。有人可以告诉我如何从编辑文本中获取电子邮件和密码,以便将它们以post方式发送到URL吗?
这是login.java代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
//import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONObject;
public class Login extends AppCompatActivity {
private EditText email,password;
private Button login;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.bLogin);
//ip azienda
//final String url = "http://192.168.10.59/mygymapi/Login.php";
//ip casa mia
final String url = "http://192.168.56.1/mygymapi/Login.php";
login.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getBaseContext(),"Response: " + response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
VolleyLog.e("Error: ", error.getMessage());
}
});
MySingleton.getInstance(getBaseContext()).addToRequestQueue(jsObjRequest);
}
});
}
}
单身人士代码:
import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.Request;
import com.android.volley.toolbox.Volley;
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
答案 0 :(得分:2)
您可以尝试以下代码。
代码
public class Login extends AppCompatActivity {
private EditText email,password;
private Button login;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.bLogin);
//ip azienda
//final String url = "http://192.168.10.59/mygymapi/Login.php";
//ip casa mia
final String url = "http://192.168.56.1/mygymapi/Login.php";
login.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
JSONObject params = new JSONObject();
String Email = email.getText().toString();
String Password = password.getText().toString();
try{
params.put("user_mail", Email);
params.put("user_pass", Password);
}
catch(JSONException e){
e.printStackTrace();
}
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
int errorcode = response.getInt("errorCode");
String errormessage = response.getString("errorMessage");
JSONObject data = response.getJSONObject("data");
String token = data.getString("token");
Toast.makeText(getBaseContext(),"Token: " + token, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
VolleyLog.e("Error: ", error.getMessage());
}
});
MySingleton.getInstance(getBaseContext()).addToRequestQueue(jsObjRequest);
}
});
}
}
答案 1 :(得分:0)
使用此功能发布数据
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
}
});
并将请求添加到请求队列
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);