Android用HttpClient请求Servlet乱码问题

CoolEgos 2014-09-12 03:03:27
Servlet的代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zf.entity.GoodsDetail;

public class WeiDaoServlet extends HttpServlet {

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("name");
if (username.equals("zhangfan")) {
/*
StringBuilder builder = new StringBuilder();
builder.append('{');
builder.append("name:").append("zhangfan").append(",").append("age:").append(22);
builder.append('}');
System.out.println("builder" + builder);
request.setAttribute("json", builder.toString());
request.getRequestDispatcher("/MyJsp.jsp").forward(request, response);
*/
String str = "张三";
response.getOutputStream().write(str.getBytes("GBK"));
// str = URLEncoder.encode(str, "UTF-8");
// response.getWriter().println(str);
}
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

}


Android端的代码:
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread()
{
public void run(){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.191.1:8080/WeiDaoHouTai/servlet/WeiDaoServlet?name=zhangfan");
// post.getParams().setParameter("", "UTF-8");
try {
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode()==200) {
// Toast.makeText(HomeActivity.this, "成功", Toast.LENGTH_SHORT).show();
// response.
HttpEntity entity = response.getEntity();
String msg = EntityUtils.toString(entity);
Log.d("zf", "entity = " + msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
// DefaultHttpClient client = new DefaultHttpClient();
// HttpPost post = new HttpPost("http://192.168.1.103:8080/WeiDaoHouTai/servlet/WeiDaoServlet?name=zhangfan");
// try {
// HttpResponse response = client.execute(post);
// if(response.getStatusLine().getStatusCode()==200) {
// Toast.makeText(HomeActivity.this, "成功", Toast.LENGTH_SHORT).show();
// }
// } catch (ClientProtocolException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}

直接在浏览器中可以得到正确的结果


而Android端得到的结果是乱码?怎么处理?
...全文
474 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
枯木_叶 2015-06-20
  • 打赏
  • 举报
回复
在doPost里面设置request.setCharacterEncoding("utf-8"); 亲自试过,绝对有效 楼主可以试一试
哎,真难 2014-09-12
  • 打赏
  • 举报
回复
建议改成json,android对中文支持性很差
lionfresh 2014-09-12
  • 打赏
  • 举报
回复
应该是将str.getBytes("GBK"));中的GBK改为UTF-8试试
新靖界 2014-09-12
  • 打赏
  • 举报
回复
UTF-8改成GBK试下

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧