安卓学习-ApacheHttpClient简介-GET请求-POST请求

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

第三方组件

安卓学习-ApacheHttpClient简介-GET请求-POST请求_第1张图片

服务器端 与上一节 相同

只放 主要代码

activity_apache_http_client.xml


xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.administrator.http.ApacheHttpClientActivity">

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HttpClient GET请求"
android:id="@+id/button4"
android:onClick="getClick"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HttpClient POST请求"
android:id="@+id/button5"
android:onClick="postClick"
android:layout_below="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

ApacheHttpClientActivity.java

package com.example.administrator.http;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;

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 java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/*两种请求方式的区别
* get:大小不能超过4KB,传输速度快,会在URL上显示,不安全
* post:大小不限制,传输速度比GET稍慢,不会再URL上显示,安全性高*/
public class ApacheHttpClientActivity extends ActionBarActivity {

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

public void getClick(View view) {
getRequest();
}

//使用apache HttpClient的GET请求
private void getRequest() {
new Thread(new Runnable() {
@Override
public void run() {
String path = "http://169.254.228.77:8080/user/LoginServlet?username=xiaofei&password=123";

//创建请求对象
HttpGet get = new HttpGet(path);
//创建HTTP客户端对象,用于发送请求
HttpClient httpClient = new DefaultHttpClient();
try {
//向服务器发送请求,并返回响应对象
HttpResponse response = httpClient.execute(get);
//获取响应的状态码
int status = response.getStatusLine().getStatusCode();
switch (status) {
case HttpStatus.SC_OK://200
//当成一个实体对象
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
System.out.println(result);
break;
case HttpStatus.SC_NOT_FOUND://404
break;
case HttpStatus.SC_INTERNAL_SERVER_ERROR://500
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}

public void postClick(View view) {
postRequest();
}

//使用apache HttpClient的POST请求
public void postRequest() {
new Thread(new Runnable() {
@Override
public void run() {
String path = "http://169.254.228.77:8080/user/LoginServlet";
//创建请求对象
HttpPost post = new HttpPost(path);

//传递参数
ArrayList params = new ArrayList();
params.add(new BasicNameValuePair("username", "xiaofei"));
params.add(new BasicNameValuePair("password", "123"));
try {
HttpEntity entity = new UrlEncodedFormEntity(params);
post.setEntity(entity);

HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(post);
int status = response.getStatusLine().getStatusCode();
switch (status) {
case HttpStatus.SC_OK://200
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
break;
case HttpStatus.SC_NOT_FOUND://404
Log.i("HttpClient", "404");
break;
case HttpStatus.SC_INTERNAL_SERVER_ERROR://500
Log.i("HttpClient", "500");
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}











转载于:https://my.oschina.net/xiaofeiandroid/blog/653942

你可能感兴趣的:(移动开发,java,python)