PhoneHttpClient

package com.example.phonehttpclient;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

Handler handler = new Handler();
EditText username;
EditText password;
Button submit;
TextView tvshow;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

username = (EditText) findViewById(R.id.edt_username);
password = (EditText) findViewById(R.id.edt_password);
submit = (Button) findViewById(R.id.btn_submit);
tvshow = (TextView) findViewById(R.id.tv_show);

submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String name = username.getText().toString();
String pwd = password.getText().toString();

String url = "http://192.168.190.1:8080/My_Service/webdate.jsp";

HttpClientThread httpThread = new HttpClientThread(url, name,
pwd, tvshow, handler);
httpThread.start();
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

package com.example.phonehttpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.os.Handler;
import android.widget.TextView;

public class HttpClientThread extends Thread {
String str;

String url;

String username;

String password;

TextView tv;

Handler handler;

public HttpClientThread(String url, String username, String password,
TextView tv, Handler handler) {
// TODO Auto-generated constructor stub
this.url = url;
this.username = username;
this.password = password;
this.tv = tv;
this.handler = handler;
}

public void doGet() {
HttpClient httpClient = new DefaultHttpClient();
// 注意,下面这一行中,我之前把链接中的"test"误写成了"text",导致调BUG调了半天没弄出来,真是浪费时间啊
String url = this.url + "?name=" + username + "&password=" + password;
// 第二步:创建代表请求的对象,参数是访问的服务器地址
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()));
StringBuffer sb = new StringBuffer();
while ((str = reader.readLine()) != null) {
sb.append(str);
}
str = sb.toString();
handler.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
tv.setText(str);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}

public void doPost() {
Properties property = System.getProperties();
property.list(System.out);

HttpClient httpClient = new DefaultHttpClient();
String httpUrl = this.url +"?name="+ username +"&password="+ password;
HttpPost request = new HttpPost(httpUrl);
// 传递参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
HttpResponse response;
HttpEntity entity;
try {
entity = new UrlEncodedFormEntity(params, "UTF-8");
request.setEntity(entity);
response = httpClient.execute(request);
// 如果返回状态为200,获得返回的结果
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = EntityUtils.toString(response.getEntity());
if (str.trim().equals("ok")) {
// 用户登录成功
handler.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
tv.setText("登陆成功");
}
});
} else {
// 用户登录失败
handler.post(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
tv.setText("登陆失败");
}
});
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void run() {
// TODO Auto-generated method stub
doPost();
}

}

 

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
android:id="@+id/edt_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/no_data" />

<EditText
android:id="@+id/edt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:hint="@string/no_data" />

<Button
android:id="@+id/btn_submit"
android:layout_width="288dp"
android:layout_height="wrap_content"
android:text="@string/submit" />

<TextView
android:id="@+id/tv_show"
android:layout_width="290dp"
android:layout_height="0dip"
android:layout_weight="1.00"
android:text="@string/hello_world" />

</LinearLayout>

 

posted @ 2017-03-01 14:47  音为  阅读(140)  评论(0编辑  收藏  举报