package com.itheima.httpclient;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
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 com.itheima.httpclient.utils.Utils; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { Handler handler = new Handler(){
@Override
public void handleMessage(android.os.Message msg) {
Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();//子线程是不能执行Toast.makeText,
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void get(View v){
EditText et_name = (EditText) findViewById(R.id.et_name);
EditText et_pass = (EditText) findViewById(R.id.et_pass);
final String name = et_name.getText().toString();
final String pass = et_pass.getText().toString();
Thread t = new Thread(){
@Override
public void run() {//URLEncoder.encode(name)是仿浏览器的中文编码,
String path = "http://192.168.13.13/Web/servlet/CheckLogin?name=" + URLEncoder.encode(name) + "&pass=" + pass;
//使用httpClient框架做get方式提交
//1.创建HttpClient对象,用于执行get请求
HttpClient hc = new DefaultHttpClient();
//2.创建httpGet对象,构造方法的参数就是网址
HttpGet hg = new HttpGet(path);//一个get请求
//3.使用客户端对象,把get请求对象发送出去
try {
HttpResponse hr = hc.execute(hg);
//拿到响应头中的状态行
StatusLine sl = hr.getStatusLine();
if(sl.getStatusCode() == 200){
//拿到响应头的实体
HttpEntity he = hr.getEntity();
//拿到实体中的内容,其实就是服务器返回的输入流
InputStream is = he.getContent();
String text = Utils.getTextFromStream(is); //发送消息,让主线程刷新ui显示text
Message msg = handler.obtainMessage();
msg.obj = text;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
} public void post(View v){
EditText et_name = (EditText) findViewById(R.id.et_name);
EditText et_pass = (EditText) findViewById(R.id.et_pass);
final String name = et_name.getText().toString();
final String pass = et_pass.getText().toString();
Thread t = new Thread(){
@Override
public void run() {
String path = "http://192.168.13.13/Web/servlet/CheckLogin";
//1.创建客户端对象
HttpClient hc = new DefaultHttpClient();
//2.创建post请求对象
HttpPost hp = new HttpPost(path); //封装form表单提交的数据
BasicNameValuePair bnvp = new BasicNameValuePair("name", name);
BasicNameValuePair bnvp2 = new BasicNameValuePair("pass", pass);
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
//把BasicNameValuePair放入集合中
parameters.add(bnvp);
parameters.add(bnvp2); try {
//要提交的数据都已经在集合中了,把集合传给实体对象
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8");
//设置post请求对象的实体,其实就是把要提交的数据封装至post请求的输出流中
hp.setEntity(entity);
//3.使用客户端发送post请求
HttpResponse hr = hc.execute(hp);
if(hr.getStatusLine().getStatusCode() == 200){
InputStream is = hr.getEntity().getContent();
String text = Utils.getTextFromStream(is);
//发送消息,让主线程刷新ui显示text
Message msg = handler.obtainMessage();
msg.obj = text;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
}
}
package com.itheima.httpclient.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; public class Utils { public static String getTextFromStream(InputStream is){ byte[] b = new byte[1024];
int len = 0;//每次read的长度
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while((len = is.read(b)) != -1){
bos.write(b, 0, len);
}
String text = new String(bos.toByteArray());
bos.close();
return text;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

android80 HttpClient框架提交数据 get方式的更多相关文章

  1. PHP---TP框架---添加数据-----有三种方式

    添加数据 添加数据有三种方式: 第一种: <?php namespace Home\Controller;//这个文件的命名空间 use Think\Controller;//use使用哪一个而 ...

  2. [Python Web]常见的 POST 提交数据的方式

    本文参考整理于:https://imququ.com/post/four-ways-to-post-data-in-http.html 简介 这里介绍了,用 POST 方法提交数据时,常见的三种方式: ...

  3. Android HttpClient框架get和post方式提交数据(非原创)

    1.fragment_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

  4. .netMVC:Web页面向后台提交数据的方式和选择

    众所周知Web前端页面主要由HTML/CSS/Javascript组成,当要通过与用户的交互实现各种功能时,就需要向后台提交一些数据或者操作.在Web世界里各种实现眼花缭乱,但究其根本,不外乎三种方式 ...

  5. Web页面向后台提交数据的方式和选择

    1.通过表单提交 这是HTML支持最传统的提交方法,需要创建表单,然后表单包含各种类型的表单元素,还要有一个提交按钮,通过提交按钮来提交到后台,这种方式提交后页面会刷新. 2.通过网页链接提交 可以在 ...

  6. HttpClient post提交数据,返回json

    // string data = "{\"uid\":515,\"timestamp\":\"2018 - 5 - 25 19:05:00\ ...

  7. HttpClient post提交数据,汉字转码

    public static String post(String url, String data) throws ClientProtocolException, IOException { Htt ...

  8. 四种常见的 POST 提交数据方式--good

    HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...

  9. 讨论HTTP POST 提交数据的几种方式

    转自:http://www.cnblogs.com/softidea/p/5745369.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PU ...

随机推荐

  1. adb logcat 查看日志

    使用 logcat 命令 查看和跟踪系统日志缓冲区的命令logcat的一般用法是: [adb] logcat [<option>] ... [<filter-spec>] .. ...

  2. [原博客] HEOI2014 行记

    HEOI: 河北省信息学竞赛省队选拔赛 HEOI数据标程下载 百度盘 http://pan.baidu.com/s/1qWx7YAo 又到了一年一度的HEOI呢. 我果然还是太弱了呢. Day0 报到 ...

  3. [转贴]C++调用openssl 的AES加密例子

    #include <stdio.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h ...

  4. ANDROID_MARS学习笔记_S05_004_过滤杂质,得到真正的加速度

    一.简介 二.代码1.java (1)MainActivity.java import android.app.Activity; import android.content.Context; im ...

  5. duilib入门简明教程 -- VS环境配置(2) Alberl

      既然是入门教程,那当然得基础点,因为搜索duilib相关资料时,发现有些小伙伴到处都是编译错误,以及路径配置错误等等,还有人不知道SVN,然后一个个文件手动下载的.     其实吧,duilib的 ...

  6. 17.2.2.1 The Slave Relay Log Slave中继日志

    17.2.2.1 The Slave Relay Log Slave中继日志 中继日志, 像binary log,有一组文件组成包含events 描述数据库的修改,和一个index文件包含所有使用过的 ...

  7. Android之读取 AndroidManifest.xml 中的数据

    转:http://www.2cto.com/kf/201208/151123.html 下来示例如何读取这些数据. 1 版本信息.应用名称 2 Appliction 的Meta-data 3 Acti ...

  8. ctagst简单应用,将Vim改造:Ctags,Taglist,Cscope,OmniCppComplete,SuperTab,Winmanager,NERDTree,MiniBufExplorer,vimrc

    vim + ctags $ ctags #给同一个目录下的所有文件建立tags 这时在tags文件所在的目录下打开源文件阅读,vim就会自动调用tags文件.如果tags文件不在当前目录下,能在命令模 ...

  9. Delphi 预编译指令 的用法

    A.3 使用条件编译指令条件编译指令是非常重要的编译指令,他控制着在不同条件下(例如,不同的操作系统)产生不同的代码.条件编译指令是包含在注释括号之内的,如下表所示.                 ...

  10. [原]RobotFrameWork(四)变量运算与Evaluate

    一.特殊变量运算: 执行结果: 二.Evaluate使用 函数释义:Evaluate是执行python表达式,并返回执行结果 示例1: 执行结果: 示例2: 执行结果: 作者:liuheng12345 ...