PhoneHttpGet
package com.example.phonehpptget;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
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.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
HttpURLConnection conn;
URL url;
InputStream is;
TextView tv;
EditText et;
Button btn;
String name;
String result;
Handler handler=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
et = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
handler=new Handler();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
name = et.getText().toString();
//String pwd = pwdText.getText().toString();
// 运行线程,使用GET方法向本地服务器发送数据
GetThread getThread = new GetThread(name);
getThread.start();
}
});
}
class GetThread extends Thread {
String name;
public GetThread(String name) {
this.name = name;
}
@Override
public void run() {
// 用HttpClient发送请求,分为五步
// 第一步:创建HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 注意,下面这一行中,我之前把链接中的"test"误写成了"text",导致调BUG调了半天没弄出来,真是浪费时间啊
String url = "http://10.0.2.2:8080/My_Service/webdate.jsp?name="
+ name;
// 第二步:创建代表请求的对象,参数是访问的服务器地址
HttpGet httpGet = new HttpGet(url);
try {
// 第三步:执行请求,获取服务器发还的相应对象
HttpResponse response = httpClient.execute(httpGet);
// 第四步:检查相应的状态是否正常:检查状态码的值是200表示正常
if (response.getStatusLine().getStatusCode() == 200) {
// 第五步:从相应对象当中取出数据,放到entity当中
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent()));
result = reader.readLine();
Log.d("HTTP", "GET:" + result);
handler.post(runnableUi);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Runnable runnableUi=new Runnable(){
@Override
public void run() {
//更新界面
tv.setText("the Content is:"+result);
}
};
}
<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" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/no_data" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_get" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/btn_get" />
</LinearLayout>