import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText value1;
private EditText value2;
private EditText value3;
private Button btn_Get;
private Button btn_Post;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
}
private void findViews() {
value1 = (EditText) findViewById(R.id.value1);
value2 = (EditText) findViewById(R.id.value2);
value3 = (EditText) findViewById(R.id.value3);
btn_Get = (Button) findViewById(R.id.btn_Get);
btn_Get.setOnClickListener(new BtnClickListener());
btn_Post = (Button) findViewById(R.id.btn_Post);
btn_Post.setOnClickListener(new BtnClickListener());
}
class BtnClickListener implements OnClickListener {
@Override
public void onClick(View view) {
String name = value1.getText().toString().trim();
String pass = value2.getText().toString().trim();
String ip = value3.getText().toString().trim();
switch (view.getId()) {
case R.id.btn_Get:
GetThread getThread = new GetThread(name, pass, ip);
getThread.start();
break;
case R.id.btn_Post:
PostThread postThread = new PostThread(name, pass, ip);
postThread.start();
break;
default:
break;
}
}
}
class GetThread extends Thread {
String name;
String pass;
String ip;
public GetThread(String name, String pass, String ip) {
this.name = name;
this.pass = pass;
this.ip = ip;
}
@Override
public void run() {
// GET方式提交是URL需要拼装 格式:http://www.xxx.xxx/xxx/?name1=xxx&name2=xxx...
String url = "";
// 创建HttpClient
HttpClient httpClient = new DefaultHttpClient();
// 创建代表请求的对象,参数是访问的服务器地址
HttpGet httpGet = new HttpGet(url);
try {
// 执行请求,获取服务器返回的响应对象
HttpResponse response = httpClient.execute(httpGet);
// 根据响应码,判断响应状态
if (response.getStatusLine().getStatusCode() == 200) {
// 从响应对象中取出数据
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
Log.d("HTTP", "GET-->" + line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class PostThread extends Thread {
String name;
String pass;
String ip;
public PostThread(String name, String pass, String ip) {
this.name = name;
this.pass = pass;
this.ip = ip;
}
@Override
public void run() {
String url = "http://58.58.178.225:333/webservice/mobileservice.asmx/Login";
// 创建HttpClient
HttpClient httpClient = new DefaultHttpClient();
// 创建代表请求的对象,参数是访问的服务器地址
HttpPost httpPost = new HttpPost(url);
// 注意此处的Name为请求是商定的固定格式 Value是用户输入的要提交数据
// 此处为分装提交的参数
NameValuePair pair1 = new BasicNameValuePair("uname", name);
NameValuePair pair2 = new BasicNameValuePair("pass", pass);
NameValuePair pair3 = new BasicNameValuePair("ipstr", ip);
ArrayList pairs = new ArrayList();
pairs.add(pair1);
pairs.add(pair2);
pairs.add(pair3);
try {
// 创建代表请求体的对象
HttpEntity requestEntity = new UrlEncodedFormEntity(pairs);
// 将请求体放置在请求对象当中
httpPost.setEntity(requestEntity);
// 执行请求,获取服务器返回的响应对象
HttpResponse response = httpClient.execute(httpPost);
// 根据响应码,判断响应状态
if (response.getStatusLine().getStatusCode() == 200) {
// 从响应对象中取出数据
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
Log.d("HTTP", "POST-->" + line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//小白做记录,还在抄代码阶段,吐槽就免了