简单分析了一下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>