求助:java 代码调用 post 类型接口 json 解析错误,怎么解决

本贴最后更新于 1375 天前,其中的信息可能已经时移世改

package demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class doPost {

public static void main(String[] args) throws ClientProtocolException, IOException  {
    //填写接口地址
    String url = "http://120.78.128.25:8766/futureloan/member/login";
    //指定接口请求方式:post
    HttpPost post = new HttpPost(url);
    //准备测试数据
    String mobilephone = "13512344321";
    String pwd = "12345678";
    List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
    parameters.add(new BasicNameValuePair("mobilephone", mobilephone));
    parameters.add(new BasicNameValuePair("pwd", pwd));
    post.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
    //准备请求头数据(如果有必要,比如cookie,content-type等)
    post.setHeader("X-Lemonban-Media-Type","lemonban.v2");
    post.setHeader("Content-Type","application/json");
   
    //发送请求,获取接口响应信息(状态码,响应报文,或某些特殊的响应数据)
    HttpClient client = HttpClients.createDefault(); //准备客户端HttpClient;多态的用法
    HttpResponse httpResponse = client.execute(post); //点击按钮;需要处理异常,暂不处理,直接抛出异常
    //状态码
    int code = httpResponse.getStatusLine().getStatusCode();
    System.out.println(code);
    //响应报文
    String result = EntityUtils.toString(httpResponse.getEntity());
    System.out.println(result);
}

}
控制台输出如下:
200
{"code":1004,"msg":"JSON parse error: Unrecognized token 'mobilephone': was expecting ('true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'mobilephone': was expecting ('true', 'false' or 'null')\n at [Source: (PushbackInputStream); line: 1, column: 13]","data":null,"copyright":"Copyright 柠檬班 ? 2017-2020 湖南省零檬信息技术有限公司 All Rights Reserved"}

1 回帖
请输入回帖内容 ...