号码归属地查询

HttpResponse mHttpResponse = null;
	try {
	HttpGet mHttpGet = new HttpGet("http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mInputPhoneNumber.getText().toString()+"&userID=");
	mHttpResponse = new DefaultHttpClient().execute(mHttpGet);
	new AlertDialog.Builder(MainActivity.this).setMessage("StatusCode is "+mHttpResponse.getStatusLine().getStatusCode()).show();
	if(mHttpResponse.getStatusLine().getStatusCode()==200){
	    result = EntityUtils.toString(mHttpResponse.getEntity());
	    Log.d("phonenumber","result is "+result);
	    SAXParserFactory mSAXParserFactory = SAXParserFactory.newInstance();
	    SAXParser mSAXParser = mSAXParserFactory.newSAXParser();
	    XMLReader mXMLReader = mSAXParser.getXMLReader();
	    PhoneNumberXml mPhoneNumberXml = new PhoneNumberXml();
	    mXMLReader.setContentHandler(mPhoneNumberXml);
	    StringReader mStringReader = new StringReader(result);
	    InputSource mInputSource = new InputSource(mStringReader);
	    mXMLReader.parse(mInputSource);
	   new AlertDialog.Builder(MainActivity.this).setMessage(PhoneNumberXml.mPhoneNumberStr).create().show();
	}else{
		Toast.makeText(MainActivity.this, "网络连接错误!", Toast.LENGTH_LONG).show();
					}
								
	} catch (ClientProtocolException e) {
	e.printStackTrace();
		} catch (IOException e) {
	e.printStackTrace();
		} catch (ParserConfigurationException e) {
		e.printStackTrace();
		} catch (SAXException e) {
		e.printStackTrace();
		}

 解析的handler

 

public class PhoneNumberXml extends DefaultHandler {
	public static String mPhoneNumberStr;
	
	@Override
	public void setDocumentLocator(Locator locator) {
		super.setDocumentLocator(locator);
		mPhoneNumberStr = "";
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		Log.d("phonenumber","xml data is "+new String(ch,start,length));
		mPhoneNumberStr += "\n"+new String(ch,start,length);
	}
	
}

 

你可能感兴趣的:(查询)