使用HttpPost请求方式调用服务器
来源:互联网 发布:js判断输入框是否为空 编辑:程序博客网 时间:2024/05/16 08:25
MainActivity中:
package com.zhidi.httppost;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private EditText et_username; private EditText et_password; private Button bt_show; String result=""; /**post请求URL地址*/ private static final String URL="http://192.168.2.3:8080/test/servlet/LoginServlet"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); bt_show.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //android4.0后的新的特性,网络数据请求时不能放在主线程中。 //9、使用线程执行访问服务器,获取返回信息后通知主线程更新UI或者提示信息。 final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 1) { //8、提示读取结果 Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show(); } } }; // 启动线程来执行任务 new Thread() { public void run() { //请求网络 loginByPost(et_username.getText().toString(), et_password.getText().toString()); Message m = new Message(); m.what = 1; // 发送消息到Handler handler.sendMessage(m); } }.start(); } }); } private void init(){ et_username=(EditText)findViewById(R.id.et_username); et_password=(EditText)findViewById(R.id.et_password); bt_show=(Button)findViewById(R.id.bt_login); } /** * 请求后台,判断登录结果。 * @param username * @param password */ public void loginByPost(String username, String password) { try { //1、根据地址创建URL对象(网络访问的url) URL url = new URL(URL); //2、url.openConnection()打开网络链接 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //3、设置请求的方式 conn.setRequestMethod("POST"); conn.setDoInput(true);//发送POST请求必须设置允许输出 conn.setDoOutput(true);//发送POST请求必须设置允许输入 //4、设置请求的头 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Charset", "utf-8"); String data = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8");//传递的数据 conn.setRequestProperty("Content-Length",String.valueOf(data.getBytes().length)); //5、获取输出流 OutputStream os = conn.getOutputStream(); os.write(data.getBytes()); os.flush(); //6、获取响应的输入流对象 InputStreamReader is = new InputStreamReader(conn.getInputStream()); BufferedReader bufferedReader = new BufferedReader(is); StringBuffer strBuffer = new StringBuffer(); String line = null; //7、读取服务器返回信息 while ((line = bufferedReader.readLine()) != null) { strBuffer.append(line); } result = strBuffer.toString(); //8、关闭InputStream、关闭http连接 is.close(); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } }}
AndroidManifest中注册网络权限 <uses-permission android:name="android.permission.INTERNET"/>
0 0
- 使用HttpPost请求方式调用服务器
- C# 使用 HttpPost 请求调用 WebService
- JAVA 调用HTTPPOST请求
- HttpPost请求方式请求数据
- 接收httpPost请求HttpEntity方式
- android通过httpPost向服务器发送请求
- android通过httpPost向服务器发送请求 .
- android通过httpPost向服务器发送请求
- HttpPost,HttpGet,HttpPut,HttpDelete请求方式
- 如何使用c语言解析httppost请求
- HttpPost+json请求---服务器中文乱码及其他
- HttpPost+json请求---服务器中文乱码及其他
- httppost 远程服务器返回错误: (400) 错误的请求错误
- java最简单的方式实现httpget和httppost请求
- Android中两种请求方式HttpGet和HttpPost验证用户登录
- C#中发送HttpPost请求来调用WebService
- C#中发送HttpPost请求来调用WebService
- C#中发送HttpPost请求来调用WebService
- 从无到有开发连麦直播技术点整理-AnyRTC
- java操作数据库(JDBC)
- python函数中 return语句 之后的语句 一定不会执行?
- iOS 极光推送小结(简单配置)
- powerdesign设计pdm时怎么增加一个text模块,用于pdm相关说明什么的?
- 使用HttpPost请求方式调用服务器
- BZOJ1590: [Usaco2008 Dec]Secret Message 秘密信息 Trie树
- JS复制到剪切板(兼容主流所有浏览器)
- hessian上传文件
- 大数相加(高精度)
- SIFT之FPGA实现--主方向计算模块
- 关于在本地搭建ftp服务器的介绍
- VS2010/MFC编程入门之三十五(菜单:菜单及CMenu类的使用)
- Java序列化与反序列化