比如说我现在把 小明 20岁上传到服务器 这也是目前已有的功能 怎么通过getResponse获得服务器返回同样的信息?就是我发小明 20岁 服务器再把我发的信息返回给我 小明 20岁 需要在我的代码上进行怎么样的修改?
我的客户端的代码public class UploadUserInformationByPostService {
public static boolean save(String title, String length) throws Exception{
String path = "http://10.100.0.6:8080/ServerForPOSTMethod/ServletForPOSTMethod";
Map<String, String> params = new HashMap<String, String>();
params.put("name", title);
params.put("age", length);
return sendPOSTRequest(path, params, "UTF-8");
} /**
 * 发送POST请求
 * @param path 请求路径
 * @param params 请求参数
 * @return
 */
private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
//  title=liming&length=30
StringBuilder sb = new StringBuilder();
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
sb.append(entry.getKey()).append("=");
sb.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
}
byte[] data = sb.toString().getBytes();

HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);//允许对外传输数据
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length+"");
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
if(conn.getResponseCode() == 200){
return true;
}
return false;
}
}
服务器端代码如下 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name= request.getParameter("name");
Connection conn = null;
java.sql.PreparedStatement pstm=null;
    name= new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
String age= request.getParameter("age");
System.out.println("name from POST method: " + name );
System.out.println("age from POST method: " + age );
}我现在就是完成了把小明 20岁发送给服务器的功能 还需要加几句话可以把信息紧跟着发回来 应该怎么继续修改呢?
我在网上看到一段代码觉得是我想要的。。但是不知道怎么实现            if(httpResponse.getStatusLine().getStatusCode()==200){ 
                //取出回应字串 
                String strResult=EntityUtils.toString(httpResponse.getEntity()); 
                tv.setText(strResult); 
            }

解决方案 »

  1.   

    放在if=200里面
     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
    String lines;
            while ((lines = reader.readLine()) != null){
                System.out.println(lines);
            }服务器:
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("小明。小明");
    out.flush();
    out.close(); if(httpResponse.getStatusLine().getStatusCode()==200)使用的是HttpClient,
      

  2.   


    前辈 我找你说的做 发现没有出现小明小明的打印 单步调试lines变量的值是????? 请问您有QQ或者邮箱么 请求指导啊
      

  3.   


    前辈 我找你说的做 发现没有出现小明小明的打印 单步调试lines变量的值是????? 请问您有QQ或者邮箱么 请求指导啊我的意思是可能存在乱码不识别的问题应该如何解决 感谢前辈的热情指点
      

  4.   

    那是就传过来了啊,服务器端把编码设置成uft-8
    response.setContentType("text/xml;charset=uft-8")