JSoup简单测试

ZC: 简单的一个Android工程 以及 如何自定义apk的图标

1、创建“Android Application Project”工程(使用Eclipse的向导 来创建)

  1.1、工程名“Lottomat”

  1.2、前面都是默认值,在“Create Activity”的界面中,选择“Empty Activity”(默认选中的是“Blank Activity”,我记得 貌似使用“Blank Activity”的话 后面会有问题 但是也记不清是什么问题了...)

2、默认创建的“activity_main.xml”("Eclipse-->工程-->res --> layout"中)无法显示 窗口界面

  解决:重启一次 Eclipse

3、工程中,找到 文件夹“libs”

  3.1、将 文件“jsoup-1.7.3.jar”,复制到“libs”中。右击“libs”中的“jsoup-1.7.3.jar”-->Build Path --> Add to Build Path

   3.1.1、ZC: 在PC版的 java应用程序/javaWeb程序 中,我们一般将外部的“?.jar”文件放在 工程目录下的文件夹“lib”中,但是 我这里测试下来,这样做是不行的...只能放在“libs”中 ...!!!

4、创建包“zz”

  4.1、本包中,创建 类TC_02_gd5in11:

package zz;

import android.annotation.SuppressLint;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

// 广东11选5 (粤11选5)
@SuppressLint("NewApi") // ZC: String.isEmpty() 需要 apk支持的AndroidSDK最小版本为9(默认为8:“android:minSdkVersion="8"”),所以要加上这个
public class TC_02_gd5in11
{
    public static void main(String[] args) throws Exception
    {
        TC_02_gd5in11 gd = new TC_02_gd5in11(0);
        System.out.println("广东11选5 :");
        if (gd.GetLastQiHao() == 0)
        {
            System.out.println("	最新一期的期号 : "+gd.FstrLastQiHao);
            if (gd.GetKaiJiangHaoMa() == 0)
                System.out.println("	最新一期的开奖号码 : "+gd.FstrLastKaiJiangHaoMa);
        }
    }
    
// ***
    public TC_02_gd5in11(int _iTimeoutMillisecond)
    {
        FiTimeoutMillisecond = _iTimeoutMillisecond;
    }
    int FiTimeoutMillisecond = 0; // HTTP请求 超时时间
    
    String FstrWebUrl = "http://trend.caipiao.163.com/gd11xuan5/";
    //String FstrWebUrl = "http://trend.caipiao.163.com/jskuai3/";
    Document Fdoc = null;
    public String FstrLastQiHao = null; // 最后一期的 期号
    public String FstrLastKaiJiangHaoMa = null; // 最后一期的 开奖号码
    
    // 获取 开奖 期号(最新的一期)
    public int GetLastQiHao() throws Exception
    {
        Connection conn = Jsoup.connect(FstrWebUrl);
        if (FiTimeoutMillisecond != 0)
            conn.timeout(FiTimeoutMillisecond);
        Fdoc = conn.get();
        FstrLastQiHao = Fdoc.getElementById("endPeriodId").val();
        if ((FstrLastQiHao == null) || (FstrLastQiHao.trim().isEmpty()))
            return -1;
        return 0;
    }
    
    // 获取 开奖号码
    public int GetKaiJiangHaoMa() throws Exception
    {        
        Element eleTBody = Fdoc.getElementById("cpdata");
        if (eleTBody == null)
            return -1;
        Elements eles = eleTBody.getElementsByAttributeValue("data-period", FstrLastQiHao);
        if (eles == null)
            return -2;
        Element ele = eles.get(0);
        if (ele == null)
            return -3;
        FstrLastKaiJiangHaoMa = ele.attr("data-award");
        if ((FstrLastKaiJiangHaoMa == null) || (FstrLastKaiJiangHaoMa.trim().isEmpty()))
            return -4;
        return 0;
    }
}

5、apk界面 (activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/btnYue5in11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="粤11选5" />

    <Button
        android:id="@+id/btnClearMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnYue5in11"
        android:text="清空消息" />

    <EditText
        android:id="@+id/edtMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnClearMsg"
            android:inputType="textMultiLine"
            android:scrollbars="vertical"
            android:gravity="top|left"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>

6、MainActivity.java

package com.example.lottomat;

import zz.TC_02_gd5in11;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity
{
    EditText FedtMsg = null;
    
    void ShowMsg(String _str)
    {
        FedtMsg.append(_str + "
");
        FedtMsg.scrollTo(0, FedtMsg.getLineCount());
    }
    
    @SuppressLint("NewApi") // ZC: "StrictMode.setThreadPolicy()"和"StrictMode.setVmPolicy"及相关的方法,需要“android:minSdkVersion”为9或者11,∴需要这一行
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // ZC: 从AndroidSDK的某一个版本开始,apk的主线程(界面线程) 不建议直接访问网络。若要直接访问网络的话,则需要下面这一段代码
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads()
            .detectDiskWrites()
            .detectNetwork() // or .detectAll() for all detectable problems
            .penaltyLog()
            .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects() // ZC: 这个要求“android:minSdkVersion”为11
            .penaltyLog()
            .penaltyDeath()
            .build());
        
        // ***

        FedtMsg = (EditText)findViewById(R.id.edtMsg);
        
        Button btnClearMsg = (Button)findViewById(R.id.btnClearMsg);
        btnClearMsg.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v)
            {
                FedtMsg.setText("");
            }
        });
        
     // 广东11选5 (粤11选5)
        Button btnYue5in11 = (Button)findViewById(R.id.btnYue5in11);
        btnYue5in11.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v)
            {
                //Editable editable = edtMsg.getText();
                if (FedtMsg == null)
                    return;

                TC_02_gd5in11 gd = new TC_02_gd5in11(1000 * 10);
                try
                {
                    ShowMsg("粤11选5 :");
                    if (gd.GetLastQiHao() == 0)
                    {
                        ShowMsg("	最新一期的期号 : "+gd.FstrLastQiHao);
                        if (gd.GetKaiJiangHaoMa() == 0)
                            ShowMsg("	最新一期的开奖号码 : "+gd.FstrLastKaiJiangHaoMa);
                    }
                } catch (Exception e) {
                    //e.printStackTrace();
                    ShowMsg("粤11选5 : 获取信息失败");
                }
            }
        });
    }
}

7、AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lottomat"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <!-- ZC: 相关的权限 -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

8、自定义 apk的图标

  8.1、图标文件放置的位置

    

   ZC: 将图标文件(例如:“z1.png”)放置到文件夹"res/drawable-hdpi"、"res/drawable-mdpi"、"res/drawable-xhdpi"、"res/drawable-xxhdpi"中(默认情况下"res/drawable-ldpi"中是没有放置图标文件的,不知道是否需要放置...我这里是没放...) (上面4个文件夹是否都需要放置 图标文件?未测试过...我这里是 都放的...)

   ZC: 图标文件的名称 只能是 a-z或者0-9 组成,并且不能以数字开头,也就是只能以小写字母开头。

  8.2、修改 AndroidManifest.xml文件中的内容:

    将“android:icon="@drawable/ic_launcher"” 改成 “android:icon="@drawable/z1"”

  8.3、将工程中所有修改都保存,然后 Eclipse-->Project-->Clean... ,将 项目清理一下

9、直接手机 运行apk测试

10、生成的apk 所在位置:

  C:Users???workspace_android工程名in

11、

原文地址:https://www.cnblogs.com/codeskilla/p/5973027.html