android之网络开发(乱码解决)
来源:互联网 发布:mac预览图片快捷键 编辑:程序博客网 时间:2024/05/01 14:21
第一种,使用HttpURLConnection 发送get请求
package com.gui.net;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.os.Bundle;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 {EditText name = null;EditText pass = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);name = (EditText) findViewById(R.id.et_name);pass = (EditText) findViewById(R.id.et_pass);}public void onClick(View view) {// TODO Auto-generated method stubfinal String n = name.getText().toString();final String p = pass.getText().toString();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {String urlPath = "http://10.1.168.57:8080/login/LoginServlet?name=" + n + "&pass=" + p;URL url = new URL(urlPath);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("GET");int code = conn.getResponseCode();if(code==HttpURLConnection.HTTP_OK){InputStream input = conn.getInputStream();final String result = getStringInStream(input);runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubToast.makeText(MainActivity.this, result, 1).show();}});}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}public String getStringInStream(InputStream input) throws IOException{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while( (len=input.read(buffer)) != -1 ){ outStream.write(buffer, 0, len); } input.close(); return outStream.toString(); }}
第二种,使用HttpURLConnection 发送post请求
package com.gui.net;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.ProtocolException;import java.net.URL;import java.net.URLEncoder;import org.apache.http.protocol.HTTP;import android.os.Bundle;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 {EditText name = null;EditText pass = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);name = (EditText) findViewById(R.id.et_name);pass = (EditText) findViewById(R.id.et_pass);}public void onClick(View view) {// TODO Auto-generated method stubfinal String n = name.getText().toString();final String p = pass.getText().toString();new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {String urlPath = "http://10.1.168.57:8080/login/LoginServlet";URL url = new URL(urlPath);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setRequestMethod("POST");String data = "name=" + URLEncoder.encode(n) + "&pass="+ URLEncoder.encode(p);conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestProperty("Content-Length", data.length() + "");conn.setDoOutput(true);OutputStream out = conn.getOutputStream();out.write(data.getBytes());int code = conn.getResponseCode();if (code == HttpURLConnection.HTTP_OK) {InputStream input = conn.getInputStream();final byte[] result = getStringInStream(input).getBytes();runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {Toast.makeText(MainActivity.this, new String(result,"utf-8"), 1).show();} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}public String getStringInStream(InputStream input) throws IOException {ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = input.read(buffer)) != -1) {outStream.write(buffer, 0, len);}input.close();return outStream.toString();}}
服务器端代码
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");String name = new String(request.getParameter("name")).trim();String pass = new String(request.getParameter("pass")).trim();if (name.equals("hpg") && pass.equals("123")) {response.getWriter().print("登录成功");} else {response.getWriter().print("登录失败");}System.out.println("用户名:" + name + " 密码:" + pass);}
0 0
- android之网络开发(乱码解决)
- Android开发之解决sqlite数据库乱码
- Android网络编程之使用get方式向服务端提交数据和乱码问题的解决
- Android开发之网络编程(一)
- Android开发之网络编程(二)
- C# 网络编程之webBrowser乱码问题及解决知识
- Android网络开发之蓝牙
- Android网络开发之HttpURLConnection
- Android开发之网络部分
- Android开发之网络请求
- Android 开发之访问网络
- Java开发中的乱码解决之终极之道
- Android 开发之异步网络请求(AsyncHttp开发)
- Java 开发乱码解决
- Java开发乱码解决
- Web开发乱码解决
- java开发乱码解决
- Anroid开发乱码解决
- 同源策略和跨域访问
- 杰瑞学Perl之文件操作(1)
- android ndk 编译方式,静态库、动态库、可执行文件
- 面向对象分析与设计课程学习之不同的分析与设计方法
- c++栈和堆的区别
- android之网络开发(乱码解决)
- iOS开发 GDB调试
- 一步步学习微软InfoPath2010和SP2010--第一章节--介绍InfoPath2010(5)--XML 101
- Ruby学习笔记_require,load
- 蓝桥杯2014年C\C++A组-蚂蚁感冒
- [01背包]NOIP2001 PJ T4 装箱问题
- Tomorrow will be a new beginning
- 由“添乘”联想到中医的脉诊
- c#异步socket通信库:FastSocket