以httpclient方式提交数据

参见文章:https://mp.csdn.net/postedit/80046568(Android登录(GET提交和POST提交)

一、首先创建一个安卓项目,设置好布局

二、GET提交方式

1、因为服务器返回的数据都是流,为了方便,我们写一个工具类StreamTools,实现流到字符串的转换

2、返回的数据变成字符串后,我们通过toast提示我们访问成功和失败,由于子线程不能修改UI,所以我们再写一个方法showToast,通过runOnUiThread修改UI

3、创建一个线程,重写run方法

new Thread(){public void run() {
		try {
				String name = et_username.getText().toString().trim();
				String pwd = et_password.getText().toString().trim();
				//[2.1]定义get方式要提交的路径    小细节 如果提交中文要对name 和 pwd 进行一个urlencode 编码 
					String path = "http://192.168.11.73:8080/login/LoginServlet?username="+URLEncoder.encode(name, "utf-8")+"&password="+URLEncoder.encode(pwd, "utf-8")+"";
					
					//[3]获取httpclient实例 
					DefaultHttpClient client = new DefaultHttpClient();
					//[3.1]准备get请求 定义 一个httpget实现 
					HttpGet get = new HttpGet(path);
					
					//[3.2]执行一个get请求  
					HttpResponse response = client.execute(get);
					
					//[4]获取服务器返回的状态码
					int code = response.getStatusLine().getStatusCode();
					if (code == 200) {
						//[5]获取服务器返回的数据   以流的形式返回 
						InputStream inputStream = response.getEntity().getContent();
						
						//[6]把流转换成字符串
					    String content = StreamTools.readStream(inputStream);	
					
					    //[7]展示结果
						showToast(content);
					}
				
				} catch (Exception e) {
					e.printStackTrace();
				}
			
		};}.start();

4、添加网络权限

 <uses-permission android:name="android.permission.INTERNET"/>

三、POST方式

1、2、4步骤同GET

3、post代码

new Thread(){public void run() {
			
			try {
				//[2]获取用户名和密码 
				String name = et_username.getText().toString().trim();
				String pwd = et_password.getText().toString().trim();
				String path = "http://192.168.11.73:8080/login/LoginServlet";
				
				//[3]以httpClient 方式进行post 提交 
				DefaultHttpClient client = new DefaultHttpClient();
				//[3.1]准备post 请求 
				HttpPost post = new HttpPost(path);

				//[3.1.0]准备parameters 
				List<NameValuePair> lists = new ArrayList<NameValuePair>();
				//[3.1.1]准备 NameValuePair 实际上就是我们要提交的用户名 和密码  key是服务器key :username
				BasicNameValuePair nameValuePair = new BasicNameValuePair("username",name);
				BasicNameValuePair pwdValuePair = new BasicNameValuePair("password",pwd);
				//[3.1.3] 把nameValuePair  和 pwdValuePair  加入到集合中
				lists.add(nameValuePair);
				lists.add(pwdValuePair);
				
				//[3.1.3]准备entity
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(lists);
				
				//[3.2]准备post方式提交的正文   以实体形式准备 (键值对形式 )
				post.setEntity(entity);
				
				
				HttpResponse response = client.execute(post);
				//[4]获取服务器返回的状态码
				int code = response.getStatusLine().getStatusCode();
				if (code == 200) {
					//[5]获取服务器返回的数据   以流的形式返回 
					InputStream inputStream = response.getEntity().getContent();
					
					//[6]把流转换成字符串
				    String content = StreamTools.readStream(inputStream);	
				    
				    //[7]展示结果
					showToast(content);
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}			
		};}.start();

项目源码:https://download.csdn.net/download/qq_35951882/10369902
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用HttpClient库进行表单提交时,你可以通过以下Java代码来实现: ```java 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.HttpPost; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class FormSubmitExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("http://example.com/form"); // 替换为实际的URL // 设置表单数据 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("username", "John")); params.add(new BasicNameValuePair("password", "password123")); try { httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpClient.execute(httpPost); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line; StringBuilder result = new StringBuilder(); while ((line = reader.readLine()) != null) { result.append(line); } System.out.println(result.toString()); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先创建一个HttpClient实例,并使用HttpPost类来发送POST请求。然后,我们设置了表单数据,将其转换为UrlEncodedFormEntity,并使用setEntity方法将其设置到HttpPost实例中。最后,我们执行POST请求,并读取响应的内容。 请确保将`http://example.com/form`替换为你要提交表单的实际URL,并根据需要修改表单字段和对应的值。还要处理可能会抛出的异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值