使用LOL盒子的数据查询简单的战斗力信息
2014-12-25 23:05
1331 查看
简单分析了一下LOL盒子战斗力查询工具网页版的请求和响应的结构,做了这么个工具。使用服务器名和角色名请求LOL盒子的服务器,从返回的网页中使用正则表达式获得战斗力的基本信息,信息在html中的位置如下。后续有时间的话可能会在这个基础上继续丰富功能做其他数据的解析和展示和查询历史记录。Activity代码
public class LOLPowerActivity extends Activity {private static final int MSG_GETPOWER_SUCCESS = 1;private static final int MSG_GETPOWER_FAIL = 2;private static final String[] servers = new String[] { "电信一", "电信二", "电信三", "电信四", "电信五","电信六", "电信七", "电信八", "电信九", "电信十", "电信十一", "电信十二", "电信十三", "电信十四" };private Handler handler;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setTitle("LOL战斗力查询");setContentView(R.layout.layout_lolpower);getActionBar().setDisplayHomeAsUpEnabled(true);createHandler();init();}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if (item.getItemId() == android.R.id.home) {finish();}return super.onOptionsItemSelected(item);}private void init() {final Spinner spServer = (Spinner) findViewById(R.id.spinnerServer);ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, servers);spServer.setAdapter(adapter);spServer.setSelection(0);final EditText etPlayer = (EditText) findViewById(R.id.editTextPlayer);Button btnSearch = (Button) findViewById(R.id.buttonSearch);btnSearch.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (spServer.getSelectedItemPosition() < 0|| etPlayer.getText().toString().length() == 0) {Toast.makeText(LOLPowerActivity.this, "条件不充分,没法查", Toast.LENGTH_LONG).show();return;}search(servers[spServer.getSelectedItemPosition()], etPlayer.getText().toString().trim());}});}private void createHandler() {final TextView tvResult = (TextView) findViewById(R.id.textViewResult);handler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case MSG_GETPOWER_SUCCESS:String strResult = (String) msg.obj;if (strResult != null) {tvResult.setText(strResult);}break;case MSG_GETPOWER_FAIL:tvResult.setText("查询失败");break;default:break;}}};}private void search(final String strServer, final String strPlayer) {new Thread(new Runnable() {@Overridepublic void run() {String strUrl = "http://lolbox.duowan.com/playerDetail.php?"+ "callback=jQuery111109943135452922434_1413010378460&token=524740466"+ "&serverName=" + URLEncoder.encode(strServer) + "&playerName="+ URLEncoder.encode(strPlayer);URL url;try {url = new URL(strUrl);HttpURLConnection http = (HttpURLConnection) url.openConnection();http.setReadTimeout(1000 * 200);InputStream is = http.getInputStream();BufferedReader isr = new BufferedReader(new InputStreamReader(is,Charset.forName("UTF-8")));StringBuilder sb = new StringBuilder();String line;while ((line = isr.readLine()) != null) {sb.append(line);}String strUseful = getUseful(sb.toString());if (strUseful == null || strUseful.length() == 0) {Message msg = Message.obtain();msg.what = MSG_GETPOWER_FAIL;handler.sendMessage(msg);} else {Message msg = Message.obtain();msg.obj = strUseful;msg.what = MSG_GETPOWER_SUCCESS;handler.sendMessage(msg);}} catch (Exception e) {e.printStackTrace();Message msg = Message.obtain();msg.what = MSG_GETPOWER_FAIL;handler.sendMessage(msg);}}}).start();}/*** 从服务器返回中提取需要的信息* @param strInput* @return*/private String getUseful(String strInput) {if (strInput == null || strInput.length() == 0) {return "";}String strReg = "[0-9]+\\s\\=\\s[0-9]+\\(基础分\\)\\s\\+\\s[0-9]+\\(胜率分\\)\\s\\+\\s[0-9]+\\(胜场分\\)";Pattern pattern = Pattern.compile(strReg);Matcher matcher = pattern.matcher(strInput);while (matcher.find()) {return matcher.group();}return "";}xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"android:layout_margin="5dp"android:orientation="vertical"tools:context=".LOLPowerActivity" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp" ><TextViewandroid:id="@+id/textViewServer"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center_vertical"android:text="服务器" /><Spinnerandroid:id="@+id/spinnerServer"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:ems="10" ></Spinner></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textViewPlayer"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="角色名" /><EditTextandroid:id="@+id/editTextPlayer"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ems="10" /><Buttonandroid:id="@+id/buttonhistory"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="历史" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/buttonSearch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查询" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textViewResult"android:layout_width="wrap_content"android:layout_height="wrap_content"android:singleLine="false" /></LinearLayout></LinearLayout>
相关文章推荐
- 使用arcpy获取简单的矢量数据信息
- 使用SQLite存储和查询数据的简单实现(源代码+注释)
- java使用jdbc查询mysql数据最简单的例子
- 【Node.js-7】MySQL数据库的简单使用、增加和查询数据
- 使用Redis的脚本功能实现Redis中数据简单查询
- 利用集合完成象数据库那样存储数据,并且可以简单查询,利用map存储学生信息
- linux中目录信息查询ls命令的简单使用方法
- 安卓开发SQlite使用执行SQL语句一些简单的处理——2.查询数据库的数据
- 使用BBED查看数据文件头(block# 1)的简单使用及查询DBID/DB_NAME等信息
- 【mysql元数据库】使用information_schema.tables查询数据库和数据表信息
- Mysql中使用存储过程返回查询多个表的数据信息
- CursorLoader简单使用例子信息查询
- multimap的简单使用 一道数据输入,查询的问题
- solrJ的使用:适用于地理信息数据的查询匹配
- javascript开发中如何使用face++,得到人脸数据(如何简单获取图片url,如何获取完整的人脸信息)
- 使用Redis的脚本功能实现Redis中数据简单查询
- 开始VS 2012中LightSwitch系列的第4部分:太多信息了!使用查询来排序和筛选数据
- java简单的从数据库查询数据使用POI导出excel文件
- Nhibernate数据查询简单使用
- 在vb中使用Iphlpapi.dll获取网络信息 第四章 第三节 实例一:网络数据流量图