查询ip归属地 小应用

快过年了,写个小玩意

 下面这个应用涉及到了一些基础东西.

1 http api调用

2 asynctask使用

3 textview 自动匹配

4 string 的spilt的正则表达式


应用截图

........截图传不上来.....


代码贴下边

public class MainActivity extends Activity {
	public static String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.4) Gecko/20091016 Firefox/3.5.4";

	private TextView text;
	private boolean isshowprogress = false;
	private List<String> mlist = new LinkedList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

		setContentView(R.layout.activity_main);
		this.setProgressBarIndeterminate(isshowprogress);

		mlist.add("11.22.33.44");
		final AutoCompleteTextView ed = (AutoCompleteTextView) this
				.findViewById(R.id.ipaddr);
		text = (TextView) this.findViewById(R.id.result);

		final ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_dropdown_item_1line, mlist.toArray());
		ed.setAdapter(adapter);

		(this.findViewById(R.id.query))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub

						final String ip = ed.getText().toString();
						if (!checkIpCorrect(ip)) {
							text.setText("格式错误");
							ed.setText("");
							return;
						}

						adapter.add(ip);

						text.setText("查询中...");

						setProgressBarIndeterminate(true);
						new myAsyncTask().execute(ip);

					}

				});
	}

	class myAsyncTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String ip = params[0];
			return queryLocation(ip);

		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			text.setText(result);
			setProgressBarIndeterminate(false);
			super.onPostExecute(result);
		}

	}

	private Boolean checkIpCorrect(String ip) {
		boolean ret = true;
		String[] ips = ip.split("[.]");
		for (String i : ips) {
			int n = Integer.parseInt(i);
			if (n < 0 || n > 255)
				return false;
		}

		return ret;

	}

	private String queryLocation(String ipaddress) {
		String url = "http://ip.taobao.com/service/getIpInfo.php?ip="
				+ ipaddress;
		URL urls;
		char[] bytes = new char[1024];
		// String str = new String();
		StringBuffer result = new StringBuffer("");

		try {
			urls = new URL(url);
			HttpURLConnection con = (HttpURLConnection) urls.openConnection();
			con.setRequestProperty("user-agent", userAgent);
			con.setRequestMethod("GET");
			InputStreamReader reader = new InputStreamReader(
					con.getInputStream(), "utf-8");
			reader.read(bytes);
			String str = String.valueOf(bytes);
			System.out.println("======str= " + str);
			JSONObject json = new JSONObject(str.toString());
			JSONObject data = json.getJSONObject("data");
			StringBuffer country = new StringBuffer(data.getString("country"));
			StringBuffer city = new StringBuffer(data.getString("city"));
			result = country.append("-").append(city);
			// InputStream input = urls.openConnection().setRequestProperty("",
			// newValue)getInputStream();

			System.out.println("======str= " + str + "=country===" + country);

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result.toString();

		// HttpURLConnection
	}

}


查询ip归属地 小应用

上一篇:UVALive - 4727 Jump


下一篇:Objective-C KVO