标签:
1 package com.example.day12_04phonenumberqurey; 2 3 import java.io.InputStream; 4 import java.net.HttpURLConnection; 5 import java.net.URL; 6 import org.apache.http.Header; 7 import org.json.JSONException; 8 import org.json.JSONObject; 9 import org.xmlpull.v1.XmlPullParser; 10 import com.cskaoyan.webutils.WebUtils; 11 import com.loopj.android.http.AsyncHttpClient; 12 import com.loopj.android.http.AsyncHttpResponseHandler; 13 import android.app.Activity; 14 import android.os.Bundle; 15 import android.os.Handler; 16 import android.os.Message; 17 import android.util.Log; 18 import android.util.Xml; 19 import android.view.View; 20 import android.widget.EditText; 21 import android.widget.TextView; 22 import android.widget.Toast; 23 24 public class MainActivity extends Activity { 25 public static final String TAG = "phonenumberqurey"; 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 } 31 32 Handler hanlder = new Handler(){ 33 @Override 34 public void handleMessage(Message msg) { 35 super.handleMessage(msg); 36 switch (msg.what) { 37 case 1: 38 //Toast.makeText(MainActivity.this, (String)msg.obj, 1).show(); 39 TextView tv_loaction = (TextView) findViewById(R.id.tv_location); 40 tv_loaction.setText((String)msg.obj); 41 break; 42 case 2: 43 Toast.makeText(MainActivity.this, (String)msg.obj, 0).show(); 44 break; 45 default: 46 break; 47 } 48 } 49 }; 50 51 public void querybyxml(View v){ 52 //先获取电话号码 53 EditText et_number = (EditText) findViewById(R.id.et_number); 54 String number = et_number.getText().toString(); 55 final String path ="http://api.k780.com:88/?app=phone.get&phone=" 56 +number+"&appkey=14114&sign=ede788f7bf8d0678e281f660654ef995&format=xml"; 57 //发请求的线程 58 Thread thread = new Thread(){ 59 public void run() { 60 //要获取的城市和运营商 61 String city =null; 62 String operator = null; 63 try { 64 65 URL url = new URL(path); 66 HttpURLConnection conn= (HttpURLConnection) url.openConnection(); 67 conn.setReadTimeout(5000); 68 conn.setConnectTimeout(5000); 69 conn.setRequestMethod("GET"); 70 conn.connect(); 71 72 if(conn.getResponseCode()==200){ 73 InputStream is= conn.getInputStream(); 74 //String text = WebUtils.gettextFromInputStream(is, null); 75 XmlPullParser pullparser = Xml.newPullParser(); 76 pullparser.setInput(is, "UTF-8"); 77 //得到读取到的某部分的类型 78 int evenType = pullparser.getEventType(); 79 while(evenType!=XmlPullParser.END_DOCUMENT){ 80 if (evenType==XmlPullParser.START_TAG) { 81 Log.i(TAG, "START_TAG"+pullparser.getName()); 82 if ("operators".equals(pullparser.getName())) { 83 operator=pullparser.nextText(); 84 } 85 if ("style_citynm".equals(pullparser.getName())) { 86 city=pullparser.nextText(); 87 } 88 } 89 evenType=pullparser.next(); 90 } 91 Log.i(TAG,operator+":"+city); 92 Message msg = hanlder.obtainMessage(); 93 msg.what=1; 94 if(city==null||operator==null) 95 msg.obj="没有查询到结果"; 96 else 97 msg.obj= city+":"+operator; 98 hanlder.sendMessage(msg); 99 } 100 else { 101 Message msg = hanlder.obtainMessage(); 102 msg.what=2; 103 msg.obj="没有链接到服务器"; 104 hanlder.sendMessage(msg); 105 } 106 } catch (Exception e) { 107 e.printStackTrace(); 108 } 109 }; 110 }; 111 thread.start(); 112 } 113 114 public void querybyjson(View v){ 115 //先获取电话号码 116 EditText et_number = (EditText) findViewById(R.id.et_number); 117 String number = et_number.getText().toString(); 118 final String path ="http://api.k780.com:88/?app=phone.get&phone=" 119 +number+"&appkey=14114&sign=ede788f7bf8d0678e281f660654ef995&format=json"; 120 //发请求的线程 121 Thread thread = new Thread(){ 122 public void run() { 123 String city =null; 124 String operator = null; 125 try { 126 URL url = new URL(path); 127 HttpURLConnection conn= (HttpURLConnection) url.openConnection(); 128 conn.setReadTimeout(5000); 129 conn.setConnectTimeout(5000); 130 conn.setRequestMethod("GET"); 131 conn.connect(); 132 if(conn.getResponseCode()==200){ 133 InputStream is= conn.getInputStream(); 134 String text = WebUtils.gettextFromInputStream(is, null); 135 JSONObject jsonObject = new JSONObject(text); 136 JSONObject result = jsonObject.getJSONObject("result"); 137 city= result.getString("style_citynm"); 138 operator = result.getString("operators"); 139 Log.i(TAG,operator+":"+city); 140 Message msg = hanlder.obtainMessage(); 141 msg.what=1; 142 if(city==null||operator==null) 143 msg.obj="没有查询到结果"; 144 else 145 msg.obj= city+":"+operator; 146 hanlder.sendMessage(msg); 147 } 148 else { 149 Message msg = hanlder.obtainMessage(); 150 msg.what=2; 151 msg.obj="没有链接到服务器"; 152 hanlder.sendMessage(msg); 153 } 154 } catch (Exception e) { 155 e.printStackTrace(); 156 } 157 }; 158 }; 159 thread.start(); 160 } 161 162 163 class Myhandler extends AsyncHttpResponseHandler { 164 @Override 165 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 166 String city =null; 167 String operator = null; 168 169 /* //String text = WebUtils.gettextFromInputStream(is, null); 170 XmlPullParser pullparser = Xml.newPullParser(); 171 ByteArrayInputStream bais= new ByteArrayInputStream(responseBody); 172 try { 173 pullparser.setInput(bais, "utf-8"); 174 //得到读取到的某部分的类型 175 int evenType = pullparser.getEventType(); 176 177 while(evenType!=XmlPullParser.END_DOCUMENT){ 178 if (evenType==XmlPullParser.START_TAG) { 179 Log.i(TAG, "START_TAG"+pullparser.getName()) ; 180 if ("operators".equals(pullparser.getName())) { 181 operator=pullparser.nextText(); 182 } 183 if ("style_citynm".equals(pullparser.getName())) { 184 city=pullparser.nextText(); 185 } 186 } 187 evenType=pullparser.next(); 188 } 189 } catch (Exception e) { 190 // TODO Auto-generated catch block 191 e.printStackTrace(); 192 } 193 */ 194 195 //jason解析 196 JSONObject jsonObject =null; 197 try { 198 jsonObject = new JSONObject(new String(responseBody)); 199 JSONObject result = jsonObject.getJSONObject("result"); 200 city = result.getString("style_citynm"); 201 operator = result.getString("operators"); 202 } catch (JSONException e) { 203 e.printStackTrace(); 204 } 205 TextView tv_loaction = (TextView) findViewById(R.id.tv_location); 206 tv_loaction.setText(city+":"+operator); 207 } 208 209 @Override 210 public void onFailure(int statusCode, Header[] headers, 211 byte[] responseBody, Throwable error) { 212 } 213 } 214 215 public void querybyxml_async(View v){ 216 EditText et_number = (EditText) findViewById(R.id.et_number); 217 String number = et_number.getText().toString(); 218 final String path ="http://api.k780.com:88/?app=phone.get&phone=" 219 +number+"&appkey=14114&sign=ede788f7bf8d0678e281f660654ef995&format=xml"; 220 221 AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); 222 asyncHttpClient.post(path, new Myhandler()); 223 } 224 225 public void querybyjson_aysnc(View v){ 226 EditText et_number = (EditText) findViewById(R.id.et_number); 227 String number = et_number.getText().toString(); 228 final String path ="http://api.k780.com:88/?app=phone.get&phone=" 229 +number+"&appkey=14114&sign=ede788f7bf8d0678e281f660654ef995&format=json"; 230 231 AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); 232 asyncHttpClient.post(path, new Myhandler()); 233 } 234 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.day12_04phonenumberqurey.MainActivity" > 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="归属地查询" /> 16 17 <EditText 18 android:id="@+id/et_number" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" /> 21 22 <Button 23 android:layout_width="fill_parent" 24 android:layout_height="wrap_content" 25 android:onClick="querybyxml" 26 android:text="查询" /> 27 28 29 <Button 30 android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:onClick="querybyjson" 33 android:text="json查询" /> 34 35 <Button 36 android:layout_width="fill_parent" 37 android:layout_height="wrap_content" 38 android:onClick="querybyxml_async" 39 android:text="使用Async-http-master xml查询" /> 40 41 <Button 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:onClick="querybyjson_aysnc" 45 android:text="使用Async-http-master json查询" /> 46 47 <TextView 48 android:id="@+id/tv_location" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:text="归属地"/> 52 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/woodrow2015/p/4520702.html