在我为自己的工作写的第一个应用的时候,很多时候实际上就是为了和互联网进行交互,信息的交互。其中包括了提交”GET“和”Post“的请求,然后就是处理响应。
《1》首先从检查网络开始,通常我们打开一个应用,在手机需要数据传送而没有连接网络时,需要提醒用户,进行网络设置,好比当我们要下载一个大文件的时候提示用户是否需要利用WIFI进行下载,所以在我们登陆客户端的时候应在客户端的登入界面进行网络的检测
。
可以为应用设置简单的登入动画,即为静止的AlphaAnimation,
spashmone只是一个简单的ImageView布局。 在动画结束处,即
public void onAnimationEnd(Animation animation)中进行跳转到登陆界面。
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutPara ms.FLAG_FULLSCREEN);
setContentView(R.layout.spashmone);
ImageView iv=(ImageView)findViewById(R.id.splashImg);
AlphaAnimation ap=new AlphaAnimation(0.1f,1.0f);
ap.setDuration(3000);
iv.startAnimation(ap);
ap.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
Intent mainIntent = new Intent(Frone.this, Login.class);
Frone.this.startActivity(mainIntent);
Frone.this.finish();
}
});
在登陆界面中我们需要在用户点击登陆Button的时候,检测网络是否连通,如果连通,则进行登陆动作,否则跳出一个对话框,询问客户是否进行网络设置。这个步骤也可以在客户端oncreate中执行,不过这样客户感觉没有那么舒服,一打开客户端就弹出对话框了,客户在接受的方面会减弱。
public void init() {
/*
* 检查网络 网络正常 启动服务
*/
if(Checknet.checknet(this)){
/*
* 网络正常判断是否可以自动登录
*
* 读取存好的SharedPreferences 这个是为了实现自动登录 记录用户用户名和密码
* 实现自动登录
*/
SharedPreferences sp=Login.this.getSharedPreferences("loginparam", Activity.MODE_PRIVATE);
String param[]=SaveloginParam.getloginparam(this);
if(param[0]!=null && !"".equals(param[0])){
// Log.i("Data", param[0]+"OKin");
user.setText(param[0]);
pass.setText(param[1]);
loginAuto.setChecked(true);
loginintomainAuto();
}else{
// Log.i("Data", param[0]+"else");
loginAuto.setChecked(false);
}
/*
* 如果没有保存用户的信息 要客户输入登录
*/
Intent intent=new Intent("com.sry.logic.MainService");
Login.this.startService(intent); //启动服务
}else{
/*
* 网络异常就弹出对话框
* 提示用户设置网络
*/
AlertDialog.Builder alerdialogbuilder=new AlertDialog.Builder(context);
alerdialogbuilder.setTitle("网络出错");
alerdialogbuilder.setMessage("请设置好网络!");
/*
* 右边的Button
*/
alerdialogbuilder.setNegativeButton("退出", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
exitApp(context);//退出应用,关掉所有的Activity
}
});
/*
* 左边的Button
*/
alerdialogbuilder.setPositiveButton("设置网络", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
//跳转到网络设置的界面(Intent)
context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
});
alerdialogbuilder.create().show();
}
}
}
《2》当网络是连接的话,我们要进行登录服务了。这里提交username和password.
1.进行GET的方式提交时,只要在url的后面appen username和password 然后打开这个url,这种方式的局限性是一般的网站会用这样简单的get方式,但通常的网站为了保护客户的信息,会使用post方式提交。
2.post方法提交。
userkey.append("userName").append('=').append(User).append('&');
userkey.append("password").append('=').append(password);
login.posttopwu(Loginurl, userkey.toString())
public boolean posttopwu(String uri,String userkey)throws Exception
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri);
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23");
httppost.setEntity(new StringEntity(userkey));
HttpResponse response;
response = httpclient.execute(httppost);
String rev = EntityUtils.toString(response.getEntity());
JSONObject obj=new JSONObject(rev);
.......//在这里判断返回来得json数据 通常如果回来的success值为true 即为登陆成功
}
必要的时候需要对url进行相应的编码
URLEncoder.encode(username, "UTF-8")
这里的listParams是含有username和password的list<User>
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
listParams, HTTP.UTF_8);
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
request.setEntity(entity);
HttpResponse response = client.execute(request);
《3》提交了数据会得到response,得到String型的数据。
pathhttpost为要请求的url。
public String getworkinfolist(String pathhttpost) throws Exception{
HttpPost httpPost=new HttpPost(pathhttpost);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response=httpClient.execute(httpPost);
StringBuilder builder=new StringBuilder();
BufferedReader bufferedReader2 = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
String str2 = "";
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
.readLine()) {
builder.append(s);
}
return builder.toString();
}
通常为了节约资源,会使用json数据型,可以将String转为json数据,再解析。
JSONObject jsonObject = new JSONObject(responed);