public class MainActivity extends Activity implements OnClickListener {
private Button button;
private TextView textView;
private String path="http://qiuming.sinaapp.com/?feed=comments-rss2";//请求路径URL
private GalHttpRequest galHttpRequest=null; //新建一个GalHttpRequest对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
button=(Button)findViewById(R.id.myButton);
textView=(TextView)findViewById(R.id.myTextView);
button.setOnClickListener(this);
}
private void gal(){ //异步请求字符串String
galHttpRequest=GalHttpRequest.requestWithURL(MainActivity.this, path);
galHttpRequest.startAsynRequestString(new GalHttpLoadTextCallBack(){
//异步都要有回调函数
@Override
public void textLoaded(String arg0) {
textView.setText(arg0); //返回内容加载到textView中r
}
});
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.myButton :
button.setText("isd");
gal();
break;
default :
break;
}
}
}
private void gotoLogin(){
/*
* 帐号:bingooo
* 12345678加密后
* WH6EuTqFiII8I8/lcEvP2w==
*
* */
showProgressDialog();
String rand = Long.toString(System.currentTimeMillis()); //获取时间戳
String unencryptingToken = rand + StaticParams.APPKEY + StaticParams.SECRET;
String md5Code = MD5Tool.getEncode32(unencryptingToken); //获得加密后的md5
final String possword;
if(camelApplication.getStringData(KEY_PASSWORD).equals(login_password.getText().toString())){
possword = camelApplication.getStringData(KEY_PASSWORD);
}else{
possword = AESCrypt.encryptTest(AESKEY, login_password.getText().toString());
}
String username = login_username.getText().toString();
TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
galHttpRequest = GalHttpRequest.requestWithURL(LoginActivity.this,StaticParams.URLPREFIXES+StaticParams.LOGIN);
galHttpRequest.setPostValueForKey("mid",username);
galHttpRequest.setPostValueForKey("pwd",possword);//"WH6EuTqFiII8I8/lcEvP2w=="
galHttpRequest.setPostValueForKey("appkey",StaticParams.APPKEY);
galHttpRequest.setPostValueForKey("rand",rand);
galHttpRequest.setPostValueForKey("request_token",md5Code);
galHttpRequest.setPostValueForKey("device_num",tm.getDeviceId());//设备id
galHttpRequest.setPostValueForKey("device_type","android");//设备类型 android 或 ios
<span style="white-space:pre"> </span>galHttpRequest.setPostValueForKey("device_name",Build.MODEL+"");//设备名字
galHttpRequest.startAsynRequestString(new GalHttpRequest.GalHttpLoadTextCallBack() {
@Override
public void textLoaded(String s) {//从服务器获得返回的String s 下面json解析s
// Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
System.out.println(s);
closeProgressDialog();
try {
JSONObject loginJson = new JSONObject(s);
if(loginJson.getString("code").equals(StaticParams.READ_SUCCESS)){
//登录成功
if(rememberPass.isChecked()){
//如果保存信息
camelApplication.setStringData(KEY_USERNAME,login_username.getText().toString());
camelApplication.setStringData(KEY_PASSWORD,possword);
}else{
//不勾选则清空
camelApplication.setStringData(KEY_USERNAME,login_username.getText().toString());
camelApplication.setStringData(KEY_PASSWORD,"");
}
UserInfoUtil.setUserData(camelApplication,loginJson);
LoginActivity.this.startActivity(new Intent(LoginActivity.this,HomeFragment.class));
finish();
}else if(loginJson.getString("code").equals(StaticParams.UPDATE_DEVICE)){
<span style="white-space:pre"> </span>Intent intent = new Intent(LoginActivity.this,UpdateDeviceActivity.class);
<span style="white-space:pre"> </span>intent.putExtra("login_username", login_username.getText().toString());
<span style="white-space:pre"> </span>startActivity(intent);
}
else{
closeProgressDialog();
Toast.makeText(LoginActivity.this,loginJson.getString("msg"),Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
closeProgressDialog();
System.out.println("json解析错误");
e.printStackTrace();
}
}
});
}