您的位置:首页 > 理论基础 > 计算机网络

android之网络开发(乱码解决)

2014-03-22 23:59 106 查看
第一种,使用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;
@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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: