会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132423个问题

post带参数请求取不到内容

2020-04-13_11-44-19.png2020-04-13_11-45-11.png

package com.bjsxt.web.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/test")
public class PostDemo {
 @RequestMapping(value = "/post",method = RequestMethod.POST)
 @ResponseBody
 public Object postTest() {
  Map<String, String> map=new HashMap<String, String>();
  map.put("msg", "Test Ok");
  
  return map;
 }
 
 @RequestMapping(value = "/post/param",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE+";chaeset=utf-8")
 @ResponseBody
 public Object postTestParam(String name,String pwd) {
  System.out.println(name+"-----------"+pwd);
  Map<String, String> map=new HashMap<String, String>();
  map.put("name", name);
  map.put("pwd", pwd);
  return map;
 }
}
package com.bjsxt.httpclient.test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import javax.sound.midi.VoiceStatus;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientTest {
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  // HttpClientTest.doGet();
  // HttpClientTest.doGetParam();
  // HttpClientTest.dePostTest();
  HttpClientTest.doPostParamTest();
 }
 /**
  * get请求不带参数
  * 
  * @throws IOException
  * @throws ClientProtocolException
  */
 public static void doGet() throws ClientProtocolException, IOException {
  // 创建一个HttpClient对象
  CloseableHttpClient client = HttpClients.createDefault();
  // 创建get请求对象,在请求中输入url
  HttpGet get = new HttpGet("http://baidu.com");
  // 发送请求,并返回响应
  CloseableHttpResponse response = client.execute(get);
  // 处理响应
  // 获取响应的状态码
  int code = response.getStatusLine().getStatusCode();
  System.out.println(code);
  // 获取响应内容
  HttpEntity entity = response.getEntity();
  String content = EntityUtils.toString(entity, "utf-8");
  System.out.println(content);
  // 关闭链接
  client.close();
 }
 /**
  * get请求带参数
  * 
  * @throws URISyntaxException
  * @throws IOException
  * @throws ClientProtocolException
  */
 public static void doGetParam() throws URISyntaxException, ClientProtocolException, IOException {
  CloseableHttpClient client = HttpClients.createDefault();
  // 创建一个封装url的对象,在该对象中可以给定请求参数
  URIBuilder builder = new URIBuilder("https://www.sogou.com/web");
  builder.addParameter("query", "西游记");
  // 创建一个get请求对象
  HttpGet get = new HttpGet(builder.build());
  // 发送请求,并返回响应
  CloseableHttpResponse response = client.execute(get);
  // 处理响应
  // 获取响应的状态码
  int code = response.getStatusLine().getStatusCode();
  System.out.println(code);
  // 获取响应内容
  HttpEntity entity = response.getEntity();
  String content = EntityUtils.toString(entity, "utf-8");
  System.out.println(content);
  // 关闭链接
  client.close();
 }
 /**
  * post请求不带参数
  * 
  * @throws IOException
  * @throws ClientProtocolException
  */
 public static void dePostTest() throws ClientProtocolException, IOException {
  CloseableHttpClient client = HttpClients.createDefault();
  HttpPost post = new HttpPost("http://localhost:8080/test/post");
  // 发送请求,并返回响应
  CloseableHttpResponse response = client.execute(post);
  // 处理响应
  // 获取响应的状态码
  int code = response.getStatusLine().getStatusCode();
  System.out.println(code);
  // 获取响应内容
  HttpEntity entity = response.getEntity();
  String content = EntityUtils.toString(entity, "utf-8");
  System.out.println(content);
  // 关闭链接
  client.close();
 }
 /**
  * post请求带参数
  * 
  * @throws Exception
  */
 public static void doPostParamTest() throws Exception {
  CloseableHttpClient client=HttpClients.createDefault();
  HttpPost post=new HttpPost("http://localhost:8080/test/post/param");
  //给定参数
  List<BasicNameValuePair>list=new ArrayList<BasicNameValuePair>();
  list.add(new BasicNameValuePair("name", "陈先森"));
  list.add(new BasicNameValuePair("pwd", "cxs"));
  //将参数做字符串的转换
  StringEntity entity=new UrlEncodedFormEntity(list,"utf-8");
  //处理响应
  CloseableHttpResponse response=client.execute(post);
  //获取响应的状态码
  int code=response.getStatusLine().getStatusCode();
  System.out.println(code);
  //获取响应内容
  HttpEntity ent=response.getEntity();
  String content=EntityUtils.toString(ent,"utf-8");
  System.out.println(content);
  //关闭链接
  client.close();
 }
 
}


JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/HttpClient 33331楼

2020-04-13 10_42_03-Window.png

这个问题我是真不知道怎么解决,百度给的方式太乱了我怕把数据改乱了

JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 33332楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 33333楼
JAVA 全系列/(旧的隐藏)第十五阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 33335楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用 33337楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Maven 33338楼

老师,我自己定义了一个ArrayList类,但是输出的时候集合中没有元素对象的都索引位置上都会显示为空,能解释一下这是怎么回事吗

package cn.zl.practice;

import java.util.Arrays;

public class MyArrayList<E> {
    private int size = 0;//元素个数
    private final Object[] DEFAULT_INITIAL_EMPTY_CAPACITY ={};//默认空数组
    private final static int INITIAL_CAPACITY = 10;//初始容量
    private Object[] elementDate;//底层数组
    //无参构造方法
    public MyArrayList(){
        this.elementDate = DEFAULT_INITIAL_EMPTY_CAPACITY;
    }
    //有参构造方法
    public MyArrayList(int initial){
        if(initial > 0){
            this.elementDate = new Object[initial];
        }else if(initial == 0){
            this.elementDate = DEFAULT_INITIAL_EMPTY_CAPACITY;
        }else{
            System.out.println("请输入正确的容量!!!");
        }
    }
    //添加方法
    public boolean add(E e){
        Object[] nelementDate = elementDate;
        add(e,nelementDate,size);
        return true;
    }
    //通过数组与数组长度添加元素对象
    public void add(E e,Object[] obj,int s){
        if(s == size){
           obj = grow(size + 1);
        }
        obj[s] = e;
        size = s + 1 ;

    }
    //通过最小容量扩充数组容量
    public Object[] grow(int miniCapacity){
        return elementDate = Arrays.copyOf(elementDate,newCapacity(miniCapacity));
        }
    //计算扩充的容量
    private int newCapacity(int miniCapacity) {
        int oldCapacity = size;//elementDate.length就会有更多的null
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity <= miniCapacity) {
            if (elementDate == DEFAULT_INITIAL_EMPTY_CAPACITY) {
                return Math.max(INITIAL_CAPACITY, miniCapacity);
            }
            if(miniCapacity < 0){
                System.out.println("请输入正确的容量!!!");
            }
            return miniCapacity;
        }
        return newCapacity;
    }
    //将元素添加到指定位置
    private void add(E e,int index){
        checkIndex(index,size);
        Object[] element = elementDate;
        int length;
        if((length = size) == (element = this.elementDate).length){
            element = grow(size + 1);
        }
        System.arraycopy(element,index,element,index+1,length-index);
        element[index] = e;
        size = length + 1;
    }
    //检查索引
    private void checkIndex(int index,int size){
        if(index < 0 || index >= size)
         throw new IndexOutOfBoundsException();
    }
    //删除指定索引的元素
    private Object remove (int index){
        checkIndex(index,size);

        Object[] e = elementDate;
        Object oldelement = e[index];
        cremove(e,index);
        return oldelement;
    }
    //删除元素方法的内部方法
    private void cremove( Object[] es,int dex){
        final int s ;
        if((s = size - 1) > dex){
            System.arraycopy(es,dex + 1,es,dex,s-dex);
        }
        es[size = s] = null;
    }
    //删除指定的元素对象
    private Object remove(Object o){
        Object obj  = o;
        for (int i = 0; i < elementDate.length ; i++) {
            if(obj.equals(elementDate[i])){
                remove(i);
            }
        }
        return obj;
    }
    //根据索引获得对象
    private Object get(int index){
        checkIndex(index,size);
        return elementDate[index];
    }
    //重写toString方法
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (int i = 0; i < elementDate.length; i++) {
           sb.append(elementDate[i] + ",");
        }
        if(size > 0)
        sb.setCharAt(sb.length() - 1,'}');
        else
            System.out.println("}");
        return sb.toString();
    }
    //获得数组元素的个数
    private int size(){
        return elementDate.length;
    }
    public static void main(String[] args) {
        //创建集合对象
        MyArrayList<String> array = new MyArrayList<>();
        //添加元素
        array.add("关羽");
        System.out.println(array);
        array.add("吕布");
        array.add("王者荣耀");
        //添加元素到指定位置
        array.add("貂蝉",2);
        System.out.println(array);
        //删除指定位置的元素
        System.out.println(array.remove(1));
        //删除指定的元素对象
        array.remove("貂蝉");
        System.out.println(array);
        //获得指定索引的元素
        System.out.println(array.get(0));
        //获得集合的元素个数
        System.out.println(array.size());
    }
}

运行结果是这样的:

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 33339楼
JAVA 全系列/(旧的隐藏)第八阶段:电商高级项目_架构/编码/电商ego-完成内容管理cms系统 33340楼
JAVA 全系列/(旧的隐藏)第八阶段:电商高级项目_架构/编码/电商ego-基于solr_实现商品数据检索 33342楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637