登录界面的代码
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import utils.SPConstant;
import utils.SPUtil;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button login;
private EditText user;
private EditText verification;
private Button obtain;
private String st_user;
private RequestQueue rq;
private RequestQueue queue;
String URL="http://101.200.221.214/api/home/sendphone";
String lUrl="http://101.200.221.214/api/home/login";
int i=60;
final Handler handler = new Handler(){
public void handleMessage(Message msg){ // handle message
switch (msg.what) {
case 1:
i--;
obtain.setText("请输入验证码" + i);
if(i > 0){
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // send message
}else{
obtain.setVisibility(View.GONE);
}
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
user = (EditText) findViewById(R.id.user);
verification = (EditText) findViewById(R.id.verification);
obtain = (Button) findViewById(R.id.obtain_verification);
obtain.setOnClickListener(this);
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.obtain_verification:
st_user = user.getText().toString().trim();
Message message = handler.obtainMessage(1); // Message
handler.sendMessageDelayed(message, 1000);
rq = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.i("。。。。。。。。。。。。。。。。。。s", s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("volleyError", "登录失败" + volleyError);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("phone", st_user);
return map;
}
};
rq.add(sr);
break;
case R.id.login:
Toast.makeText(MainActivity.this, "点击le", Toast.LENGTH_SHORT).show();
queue = Volley.newRequestQueue(this);
final String st_verification = verification.getText().toString().trim();
final String dt="AmrtVGzuVC6eBz7-H5gFWT4Kgp31dko4NhD_3UzZyfFB";
Log.i("st_user",st_user);
StringRequest loginsr = new StringRequest(Request.Method.POST, lUrl, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
JSONObject object=new JSONObject(s);
String requset= object.getString("message");
String ss=object.getString("result");
SPUtil.put(MainActivity.this, SPConstant.REQUSET,requset);
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent =new Intent(MainActivity.this,Login.class);
startActivity(intent);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.i("volleyError","登录失败了"+volleyError);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//服务器的接口,需要的参数
Map<String, String> map = new HashMap<>();
map.put("phone", st_user);
map.put("code", st_verification);
map.put("type", "1");
map.put("devicetoken", dt);
return map;
}
};
queue.add(loginsr);
break;
}
}
}
token请求放在广告页
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import utils.SPConstant;
import utils.SPUtil;
/**
* Created by liaowei on 2016/7/15.
*/
public class token extends Activity {
String Url="http://101.200.221.214/api/home/islogin";
protected void onCreate(Bundle saveIntancetion){
super.onCreate(saveIntancetion);
setContentView(R.layout.activity_token);
final String st=SPUtil.getString(token.this,SPConstant.REQUSET);
Log.i("st",st);
RequestQueue tt = Volley.newRequestQueue(this);
StringRequest srt=new StringRequest(Request.Method.POST,Url,new Response.Listener<String>(){
@Override
public void onResponse(String s) {
try {
JSONObject object=new JSONObject(s);
String token1 =object.getString("message");
String result =object.getString("result");
Log.i("result",result);
if(result.equals(true+"")){
Intent intent=new Intent(token.this,Login.class);
startActivity(intent);
}else{
Intent intent=new Intent(token.this,MainActivity.class);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
//把保存的token值给到服务器
map.put("token",st);
return map;
}
};
tt.add(srt);
}
}