手机归属地(整个代码)
package com.example.day5_httpclient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText ed_phnum;
private TextView tv_show;
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
String ccontent=(String) msg.obj;
tv_show.setText(ccontent);
break;
case 2:
Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_phnum = (EditText) findViewById(R.id.editText1);
tv_show=(TextView)findViewById(R.id.textView1);
}
// http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=15850781443
/**注意:1.请求网络的耗时操作在子线程进行;
* 2.访问网络要加权限:permission INTERNET
* 3.只有主线程能更改UI,Toast也属于更改UI
* 4.注意参数名不能写错(chgmobile,否则能请求到数据,但是ERROR数据不全.得不到)
* 5.toString("gb2312");类型与网上请求到的要一致
*/
public void httpclientandhttppost(View v) {
new Thread() {
public void run() {
try {
//创建HttpClient对象(---DefaultHttpClient是HttpClient接口的实现类)
HttpClient httpClient=new DefaultHttpClient();
//网址
String path = "http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi";
//创建HttpPost对象并设置网址
HttpPost httpPost = new HttpPost(path);
//创建键值对类型的list集合(参数)
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
//添加参数信息
parameters.add(new BasicNameValuePair("chgmobile", ed_phnum
.getText().toString().trim()));
//创建实体内容对象
HttpEntity entity = new UrlEncodedFormEntity(parameters,
"utf-8");
//设置请求的实体内容
httpPost.setEntity(entity);
//执行post请求
HttpResponse httpResponse = httpClient.execute(httpPost);
//从状态行中获取状态码
int statusCode = httpResponse.getStatusLine().getStatusCode();
//判断状态码 成功
if(statusCode==200){
//获取请求的内容
InputStream inputStream = httpResponse.getEntity().getContent();
ByteArrayOutputStream bytearrayoutputstream=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len;
while((len=inputStream.read(b))!=-1){
bytearrayoutputstream.write(b, 0, len);
}
String content = bytearrayoutputstream.toString("gb2312");
System.out.println("请求到的内容"+content);
handler.obtainMessage(1, content).sendToTarget();
//handler.sendMessage(Message.obtain(handler, 1, content));
}else{
//请求失败
System.out.println("请求失败");
handler.sendMessage(Message.obtain(handler, 2, "请求数据失败"));
}
} catch (UnsupportedEncodingException e) {
handler.sendMessage(Message.obtain(handler, 3, "UnsupportedEncodingException"));
e.printStackTrace();
} catch (ClientProtocolException e) {
handler.sendMessage(Message.obtain(handler, 4, "ClientProtocolException"));
e.printStackTrace();
} catch (IOException e) {
handler.sendMessage(Message.obtain(handler, 5, "IOException"));
e.printStackTrace();
}
};
}.start();
}
}