使用HttpURLConnection採用get方式請求資料-----亂碼問題
阿新 • • 發佈:2019-01-09
1. 在子執行緒中改變ui,用handle通訊,還可以用
// 執行任務在主執行緒中,除handle外
runOnUiThread(new Runnable() {
@Override
public void run() {
// 就是在主執行緒中操作
Toast.makeText(MainActivity.this, state, 0).show();
}
}
2. ******根據流返回一個字串;程式碼---
3. 新增許可權Internet
4. 亂碼問題:
MyEclipse:預設採用的是iso8859-1
模擬器預設採用的是utf-8
4. 伺服器方面:
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ String username =request.getParameter(“username”);//採用的編碼是iso8859-1 String password =request.getParameter(“password”); //採用iso8859-1的編碼對姓名進行逆轉,轉換成位元組陣列,再使用utf-8編碼對資料進 //行轉換成字串 username=new String(username.getBytes(“iso8859-1”),”utf-8”); password=new String(password.getBytes(“iso8859-1”),”utf-8”); If(“李四”.equals(username)&&”123”.equals(password)){ //getBytes預設情況下,使用的iso8859-1的編碼,但如果發現碼錶中沒有當前字元,會//使用當前系統的預設編碼:GBK Response.getOutputStream().write(“登陸成功”.getBytes()); }else{ Response.getOutputStream().write(“登陸失敗”.getBytes()); } }
5.客戶端訪問:localhost:8080/ServerItheima28/servlet/LoginServlet?username=lisi&password=123
6.程式碼
public class MainActivity extends Activity { private EditText etUserName; private EditText etPassword; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUserName = (EditText) findViewById(R.id.et_username); etPassword = (EditText) findViewById(R.id.et_password); } public void doGet(View v) { final String userName = etUserName.getText().toString(); final String password = etPassword.getText().toString(); new Thread( new Runnable() { @Override public void run() { // 使用get方式抓去資料,返回登陸結果 final String state = NetUtils.loginOfGet(userName, password); // 執行任務在主執行緒中,除handle外 runOnUiThread(new Runnable() { @Override public void run() { // 就是在主執行緒中操作 Toast.makeText(MainActivity.this, state, 0).show(); } }); } }).start(); }
loginOfGet方法:
public class NetUtils {
/**
* 使用get的方式登入
* @param userName
* @param password
* @return 登入的狀態
*/
public static String loginOfGet(String userName, String password) {
HttpURLConnection conn = null;
try {
String data = "username=" + URLEncoder.encode(userName) + "&password=" + URLEncoder.encode(password);
URL url = new URL("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet?" + data);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); // get或者post必須得全大寫
conn.setConnectTimeout(10000); // 連線的超時時間
conn.setReadTimeout(5000); // 讀資料的超時時間
int responseCode = conn.getResponseCode();
if(responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(TAG, "訪問失敗: " + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.disconnect(); // 關閉連線
}
}
return null;
}
/**
* 根據流返回一個字串資訊
* @param is
* @return
* @throws IOException
*/
private static String getStringFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
is.close();
String html = baos.toString(); // 把流中的資料轉換成字串, 採用的編碼是: utf-8
// String html = new String(baos.toByteArray(), "GBK");
baos.close();
return html;
}
}