一、splash界面检查网络
1.判断网络连接状态:
private boolean isNetworkConnected(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info =cm.getActiveNetworkInfo();
if(info!=null&&info.isConnected()){
return true;
}else {
return false ;
}
}
2. 获取软件版本:
private String getVersion() {
PackageManager pm = getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
return info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
二、主界面(TabActivity)
xml基本布局,TabHost android:id="@android:id/tabhost"必须包括<TabWidget android:id="@android:id/tabs"....和<FrameLayout android:id="@android:id/tabcontent"...两个组件
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@android:id/tabhost"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_alignParentBottom="true" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="60dip" >
</FrameLayout>
</RelativeLayout>
</TabHost>
java文件里面添加tabView(关键类TabSpec)
mTabHost = getTabHost();
TabSpec myDoubanTab = mTabHost.newTabSpec("我的豆瓣");
//指定要显示的具体内容
Intent intent = new Intent(this,MyDoubanActivity.class);
myDoubanTab.setContent(intent);
//设置显示的标题内容
myDoubanTab.setIndicator(getTabView(R.drawable.tab_main_nav_me, "我的豆瓣"));
mTabHost.addTab(myDoubanTab);
三、tabView相关内容
1.listView
xml里listview divider设置listitem之间的间隔
<ListView
android:id="@+id/melistview"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_marginLeft="5.0dip"
android:layout_marginRight="3.0dip"
android:layout_weight="1.0"
android:cacheColorHint="#00000000"
android:divider="@android:color/transparent"
android:dividerHeight="5.0dip"
android:listSelector="@android:color/transparent"
android:paddingTop="5.0dip"
android:scrollbarStyle="outsideInset" />
四、模拟网页登陆、获取验证码
public class NetUtil {
private static final String loginserver = "http://www.douban.com/accounts/login";
/**
*
* @param username
* 用户名
* @param password
* 用户密码
* @return
*/
public static List<String> getAccessToken(String username, String password,
String captcha_id, String captcha_solution) {
try {
String apiKey = "078409f18961d372168c1dd49c257a56";
String secret = "0dd3419e39fe4b2c";
DoubanService myService = new DoubanService("黑马6的小豆瓣", apiKey,
secret);
// 1.模拟用户登陆豆瓣官网的行为->主要是为了回去豆瓣登陆成功的cookie
// httppost的请求 ,最方便的就是采用httpclient 开源的jar包
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(loginserver);
// 设置post信息对应的数据实体
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("source", "simple"));
parameters.add(new BasicNameValuePair("redir",
"http://www.douban.com"));
parameters.add(new BasicNameValuePair("form_email",
"itcastweibo@sina.cn"));
parameters.add(new BasicNameValuePair("form_password", "a11111"));
if (TextUtils.isEmpty(captcha_solution)
|| TextUtils.isEmpty(captcha_id)) {
}else{
parameters.add(new BasicNameValuePair("captcha-solution",
captcha_solution));
parameters
.add(new BasicNameValuePair("captcha-id", captcha_id));
}
parameters.add(new BasicNameValuePair("user_login", "登陆"));
post.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
HttpResponse response = client.execute(post);
System.out.println(response.getStatusLine().getStatusCode());
InputStream is = response.getEntity().getContent();
// 把服务器返回回来的inputstream里面的内容 包装成一个html的对象
Source source = new Source(is);
System.out.println(source.toString());
CookieStore cookies = client.getCookieStore();
/*
* //新开了一个浏览器,把刚才登陆成功的cookie信息设置给这个新开的浏览器 DefaultHttpClient client2
* = new DefaultHttpClient(); client2.setCookieStore(cookies);
*/
String path = myService.getAuthorizationUrl(null);
// 模拟用户的同意的点击事件
HttpPost agreePost = new HttpPost(path);
List<BasicNameValuePair> agreeParameters = new ArrayList<BasicNameValuePair>();
agreeParameters.add(new BasicNameValuePair("ck", "wViC"));
int start = path.lastIndexOf("=") + 1;
String oauth_token = path.substring(start, path.length());
agreeParameters.add(new BasicNameValuePair("oauth_token",
oauth_token));
agreeParameters.add(new BasicNameValuePair("oauth_callback", ""));
agreeParameters.add(new BasicNameValuePair("ssid", "8e4a1f5b"));
agreeParameters.add(new BasicNameValuePair("confirm", "同意"));
agreePost.setEntity(new UrlEncodedFormEntity(agreeParameters,
"UTF-8"));
client.execute(agreePost);
ArrayList<String> tokens = myService.getAccessToken();
// 3.授权成功后 返回两把钥匙, 后门的钥匙
System.out.println("accesstoken " + tokens.get(0));
System.out.println("token secret " + tokens.get(1));
return tokens;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 判断是否需要输入验证码
*
* @return
* @throws Exception
*/
public static boolean needCaptcha() throws Exception {
boolean result = false;
URL url = new URL("http://www.douban.com/accounts/login");
URLConnection conn = url.openConnection();
Source source = new Source(conn);
List<Element> inputs = source.getAllElements("input");
for (Element input : inputs) {
// System.out.println(input.getAttributeValue("name"));
if ("captcha-id".equals(input.getAttributeValue("name"))) {
result = true;
return result;
}
}
return false;
}
/**
* 获取验证码对应的bitmap
*/
public static CaptchaBean getCaptchaBitmap() throws Exception {
URL url = new URL("http://www.douban.com/accounts/login");
CaptchaBean bean = new CaptchaBean();
URLConnection conn = url.openConnection();
Source source = new Source(conn);
List<Element> inputs = source.getAllElements("input");
for (Element input : inputs) {
// System.out.println(input.getAttributeValue("name"));
if ("captcha-id".equals(input.getAttributeValue("name"))) {
String id = input.getAttributeValue("value");
String path = "http://www.douban.com/misc/captcha?id=" + id
+ "&size=s";
URL iconurl = new URL(path);
InputStream is = iconurl.openStream();
bean.setBitmap(BitmapFactory.decodeStream(is));
bean.setId(id);
return bean;
}
}
return null;
}
五、获取信息
异步
/**
* 完成数据和事件的处理
*/
public void setupView() {
new AsyncTask<Void, Void, UserEntry>(){
/**在后台任务执行之前被调用 运行在ui线程里面的
*/
@Override
protected void onPreExecute() {
loading.setVisibility(View.VISIBLE);
super.onPreExecute();
}
/**
* 在后台任务执行完毕后调用,运行在ui线程里面的
*/
@Override
protected void onPostExecute(UserEntry result) {
super.onPostExecute(result);
loading.setVisibility(View.INVISIBLE);
if(result!=null){
txtUserAddress.setText( result.getLocation());
txtUserDescription.setText( ((TextContent) result.getContent()).getContent().getPlainText());
txtUserName.setText( result.getTitle().getPlainText());
}else{
MyToast.showToast(getApplicationContext(), "获取数据失败", 1);
}
}
/**
* 在子线程里面执行的任务 可以运行一些耗时的操作
*/
@Override
protected UserEntry doInBackground(Void... params) {
try {
String id = myService.getAuthorizedUser().getUid();
UserEntry ue = myService.getUser(id);
return ue;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}.execute();
}