1 package com.highxin.number_location; 2 3 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.util.regex.Matcher; 9 import java.util.regex.Pattern; 10 import org.apache.http.HttpResponse; 11 import org.apache.http.ParseException; 12 import org.apache.http.client.ClientProtocolException; 13 import org.apache.http.client.methods.HttpGet; 14 import org.apache.http.impl.client.DefaultHttpClient; 15 import org.apache.http.util.EntityUtils; 16 import android.app.Activity; 17 import android.database.Cursor; 18 import android.database.sqlite.SQLiteDatabase; 19 import android.os.Bundle; 20 import android.os.Handler; 21 import android.os.Message; 22 import android.text.Editable; 23 import android.text.TextWatcher; 24 import android.view.View; 25 import android.view.View.OnClickListener; 26 import android.widget.Button; 27 import android.widget.EditText; 28 import android.widget.TextView; 29 import android.widget.Toast; 30 31 public class MainActivity extends Activity implements Runnable { 32 private SQLiteDatabase database; 33 private final String DATABASE_PATH = android.os.Environment 34 .getExternalStorageDirectory().getAbsolutePath() 35 + "/mobilelocation"; 36 private String number; 37 private String city; 38 private String location; 39 private final String DATABASE_FILENAME = "mobilelocation.db"; 40 private TextView tv; 41 private String baseURL = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"; 42 String myurl=""; 43 String myresult=""; 44 String temp=""; 45 Thread t; 46 private Handler mHandler = new Handler() { 47 48 @Override 49 public void handleMessage(Message msg) { 50 super.handleMessage(msg); 51 myresult = filterHtml(temp); 52 tv.setText(myresult); 53 } 54 55 }; 56 @Override 57 protected void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 setContentView(R.layout.activity_main); 60 final EditText et = (EditText) findViewById(R.id.et_number); 61 Button bt = (Button) findViewById(R.id.bt_srarch); 62 //ImageView iv = (ImageView) findViewById(R.id.iv_delete); 63 tv =(TextView) findViewById(R.id.tv_loaction); 64 65 66 67 database = openDatabase(); 68 et.addTextChangedListener(new TextWatcher() { 69 70 @Override 71 public void onTextChanged(CharSequence s, int start, int before, int count) { 72 // TODO Auto-generated method stub 73 74 } 75 76 @Override 77 public void beforeTextChanged(CharSequence s, int start, int count, 78 int after) { 79 // TODO Auto-generated method stub 80 81 } 82 83 @Override 84 public void afterTextChanged(Editable s) { 85 if(!isNumeric(s.toString()) || s.length()>11 ) { 86 Toast.makeText(MainActivity.this, "您输入的号码有误,请查证后再输!!", Toast.LENGTH_SHORT).show(); 87 return; 88 } 89 if(s.length()<7){ 90 return; 91 } 92 if(s.length()>=7 && s.length()<=11) { 93 s= (Editable) s.subSequence(0, 7); 94 } 95 96 // TODO Auto-generated method stub 97 Cursor cursor = database.rawQuery( 98 "select city,location from location_data where number=?", 99 new String[] 100 {s.toString()}); 101 if (cursor.getCount() > 0) 102 { 103 // 必须使用moveToFirst方法将记录指针移动到第1条记录的位置 104 cursor.moveToFirst(); 105 city = cursor.getString(cursor.getColumnIndex("city")); 106 location = cursor.getString(cursor.getColumnIndex("location")); 107 //将结果显示到TextView中 108 tv.setText(et.getText()+"\n"+city.toString()+"\n"+location.toString()); 109 } 110 else { 111 //将结果显示到TextView中 112 tv.setText(""); 113 Toast.makeText(MainActivity.this, "点击查询进行联网搜索", Toast.LENGTH_SHORT).show(); 114 } 115 } 116 }); 117 bt.setOnClickListener(new OnClickListener() { 118 @Override 119 public void onClick(View v) { 120 String result =""; 121 number = et.getText().toString(); 122 String b=""; 123 myurl = baseURL+"?mobileCode="+number+"&userID="+b; 124 t = new Thread(MainActivity.this); 125 t.start(); 126 System.out.println(myurl); 127 } 128 }); 129 } 130 131 private String OpenHttpConnection(String url) 132 { 133 String result = null; 134 HttpGet httpGet = new HttpGet(url); 135 HttpResponse httpResponse = null; 136 try { 137 httpResponse = new DefaultHttpClient().execute(httpGet); 138 } catch (ClientProtocolException e1) { 139 e1.printStackTrace(); 140 } catch (IOException e1) { 141 e1.printStackTrace(); 142 } 143 if (httpResponse.getStatusLine().getStatusCode() == 200) 144 { 145 //第三步,使用getEntity方法活得返回结果 146 try { 147 result = EntityUtils.toString(httpResponse.getEntity()); 148 } catch (ParseException e) { 149 e.printStackTrace(); 150 } catch (IOException e) { 151 e.printStackTrace(); 152 } 153 } 154 return result; 155 } 156 157 private SQLiteDatabase openDatabase() { 158 try 159 { 160 // 获得dictionary.db文件的绝对路径 161 String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; 162 File dir = new File(DATABASE_PATH); 163 // 如果/sdcard/dictionary目录中存在,创建这个目录 164 if (!dir.exists()) 165 dir.mkdir(); 166 // 如果在/sdcard/dictionary目录中不存在 167 // dictionary.db文件,则从res\raw目录中复制这个文件到 168 // SD卡的目录(/sdcard/dictionary) 169 if (!(new File(databaseFilename)).exists()) 170 { 171 // 获得封装dictionary.db文件的InputStream对象 172 InputStream is = getResources().openRawResource( 173 R.raw.mobilelocation); 174 FileOutputStream fos = new FileOutputStream(databaseFilename); 175 byte[] buffer = new byte[50000]; 176 int count = 0; 177 // 开始复制dictionary.db文件 178 while ((count = is.read(buffer)) > 0) 179 { 180 fos.write(buffer, 0, count); 181 } 182 //关闭文件流 183 fos.close(); 184 is.close(); 185 } 186 // 打开/sdcard/dictionary目录中的dictionary.db文件 187 SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase( 188 databaseFilename, null); 189 return database; 190 } 191 catch (Exception e) 192 { 193 } 194 //如果打开出错,则返回null 195 return null; 196 } 197 //判断字符串是否为数字 198 public boolean isNumeric(String str){ 199 Pattern pattern = Pattern.compile("[0-9]*"); 200 Matcher isNum = pattern.matcher(str); 201 if( !isNum.matches() ){ 202 return false; 203 } 204 return true; 205 } 206 //过滤掉<>中的无用信息 207 private String filterHtml(String source) { 208 if (null == source) { 209 return ""; 210 } 211 return source.replaceAll("</?[^>]+>", "").trim(); 212 } 213 @Override 214 public void run() { 215 temp=OpenHttpConnection(myurl); 216 mHandler.sendEmptyMessage(0); 217 } 218 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <RelativeLayout 6 android:id="@+id/search" 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" > 9 <EditText 10 android:id="@+id/et_number" 11 android:layout_marginTop="1dp" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:background="@android:drawable/edit_text" 15 android:hint="请输入手机号" 16 android:layout_alignParentLeft="true" 17 android:layout_toLeftOf="@+id/bt_search" 18 android:ems="11" 19 android:singleLine="true" /> 20 <!-- <ImageView 21 android:id="@+id/iv_delete" 22 android:layout_width="10dp" 23 android:layout_height="10dp" 24 android:scaleType="centerCrop" 25 android:src="@drawable/delete" 26 android:visibility="invisible" 27 /> --> 28 <Button 29 android:id="@+id/bt_srarch" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_alignParentRight="true" 33 android:layout_alignParentTop="true" 34 android:text="搜索" 35 /> 36 37 </RelativeLayout> 38 39 40 <TextView 41 android:id="@+id/tv_loaction" 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:text="" /> 45 46 </LinearLayout>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET"/>
时间: 2024-11-25 05:42:32