使用LOL盒子的数据查询简单的战斗力信息

使用LOL盒子的数据查询简单的战斗力信息简单分析了一下LOL盒子战斗力查询工具网页版的请求和响应的结构,做了这么个工具。使用服务器名和角色名请求LOL盒子的服务器,从返回的网页中使用正则表达式获得战斗力的基本信息,信息在html中的位置如下。后续有时间的话可能会在这个基础上继续丰富功能做其他数据的解析和展示和查询历史记录。Activity

大家好,欢迎来到IT知识分享网。

简单分析了一下LOL盒子战斗力查询工具网页版的请求和响应的结构,做了这么个工具。使用服务器名和角色名请求LOL盒子的服务器,从返回的网页中使用正则表达式获得战斗力的基本信息,信息在html中的位置如下。

使用LOL盒子的数据查询简单的战斗力信息

后续有时间的话可能会在这个基础上继续丰富功能做其他数据的解析和展示和查询历史记录。

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;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("LOL战斗力查询");
		setContentView(R.layout.layout_lolpower);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		createHandler();
		init();
	}

	@Override
	public 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() {

			@Override
			public 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() {

			@Override
			public 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() {

			@Override
			public 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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp" >

        <TextView
            android:id="@+id/textViewServer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="服务器" />

        <Spinner
            android:id="@+id/spinnerServer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:ems="10" >
        </Spinner>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewPlayer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="角色名" />

        <EditText
            android:id="@+id/editTextPlayer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

        <Button
            android:id="@+id/buttonhistory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="历史" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/buttonSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false" />
    </LinearLayout>

</LinearLayout>

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/34867.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信