布局代码--线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="请输入电话号码" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="电话号码"
android:id="@+id/ed"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
android:onClick="click"
android:id="@+id/btn"/>
</LinearLayout>
主线程代码
package csdn.com;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText ed;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void click(View v){
//获取电话号码
ed = (EditText) findViewById(R.id.ed);
//获取按钮对象
btn = (Button) findViewById(R.id.btn);
String url="http://www.096.me/api.php?phone="+ed.getText().toString().trim()+"&mode=txt";
System.out.println(url);
a aa=new a(getApplicationContext(),btn);
aa.execute(url);
}
}
自定义class类
package csdn.com;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Button;
import android.widget.Toast;
public class a extends AsyncTask<String, Void, String>{
private Button btn;
private Context context;
String getStr="";
public a(Context _context,Button _btn){
this.btn=_btn;
this.context=_context;
}
@Override
protected void onPreExecute() {
btn.setText("查询中...");
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
Thread.sleep(3000);
//进行网络连接
HttpClient client=new DefaultHttpClient();
//定义get请求对象。
HttpGet get=new HttpGet(params[0]);
//发送get请求,并且返回响应的结果
HttpResponse response=client.execute(get);
//判断响应完成
if(response.getStatusLine().getStatusCode()==200){
//响应的实体
HttpEntity he=response.getEntity();
//读取返回的数据流
InputStream is=he.getContent();
getStr=new String(getByteData(is),"GBK");
System.out.println("----------"+getStr);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return getStr;
}
@Override
protected void onPostExecute(String result) {
btn.setText("查询");
Toast.makeText(context, result, 10).show();
super.onPostExecute(result);
}
public static byte[] getByteData(InputStream is){
//声明一个ByteArrayOutputStream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
//定义缓冲区
byte[] buffer = new byte[1024];
//读取is中的数据,写入到bos中
int len = -1;
while ((len = is.read(buffer)) != -1) {
//写入到bos中
bos.write(buffer, 0, len);
}
is.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
return bos.toByteArray();
}
}