android之网络开发(乱码解决)
2014-03-22 23:59
106 查看
第一种,使用HttpURLConnection 发送get请求
第二种,使用HttpURLConnection 发送post请求
服务器端代码
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; @Override protected 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 stub final String n = name.getText().toString(); final String p = pass.getText().toString(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { 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() { @Override public void run() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, result, 1).show(); } }); } } catch (Exception e) { // TODO Auto-generated catch block e.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; @Override protected 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 stub final String n = name.getText().toString(); final String p = pass.getText().toString(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { 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() { @Override public void run() { // TODO Auto-generated method stub try { Toast.makeText(MainActivity.this, new String(result,"utf-8"), 1).show(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } } catch (Exception e) { // TODO Auto-generated catch block e.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); }
相关文章推荐
- Android开发之解决sqlite数据库乱码
- 【Android】连接网络读取文件内容中文乱码解决办法
- Android 与 PC网络通信之中文乱码问题解决
- 解决Android开发中加载html页面乱码的问题
- Android开发:休眠唤醒或开机后cmwap/cmnet网络不能连接的解决办法
- Android 与 PC网络通信之中文乱码问题解决
- Android 从网络中获取数据时 产生部分数据乱码的解决
- Android 从网络中获取数据时 产生部分数据乱码的解决
- Android 从网络中获取数据时 产生部分数据乱码的解决
- android开发中在eclipse中导入一个项目时出现中文乱码的解决方法
- Android开发八 “尚未注册网络”错误信息的解决办法
- android 获取网络数据,回传到本地用TextView显示乱码问题解决方法
- delphi xe5 android 开发数据访问手机端 解决乱码的办法
- XE5 Android 开发数据访问手机端 解决乱码的办法
- Android网络编程之使用get方式向服务端提交数据和乱码问题的解决
- Android 开发中Eclipse中文乱码的解决办法
- android开发时,http提交地址中有中文乱码的解决方法
- Android网络开发中出现NoHttpResponseException 或者ClientProtocolException解决方法
- XE5 Android 开发数据访问手机端 解决乱码的办法 [转]
- android开发中 解决服务器端解析MySql数据时中文显示乱码的情况