zoukankan      html  css  js  c++  java
  • java后台调用接口并获取返回值

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import org.apache.http.HttpEntity;
    import org.apache.http.NameValuePair;
    import org.apache.http.StatusLine;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    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;
    import org.springframework.data.repository.query.QueryMethod;
    import com.alibaba.fastjson.JSON;
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.flatform.fklc.domain.FkLogin;
    import java.io.IOException;
    public class EntityZDUtil {
    //连接的网址,这里假装一个
    private static final String SERVICE_URL = "http://192.122.91.111:8080/test/tester/tttest.do";
    
    
    //方法
    public static String Test() {
      CloseableHttpClient httpClient = HttpClients.createDefault();
      try {
        HttpPost httpPost = new HttpPost(SERVICE_URL);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        //根据需要传输的参数建立实体FkLogin
        //需要的数据格式为
        data={
        "uname": "admin",
        "upwd": "12345"
        }
          FkLogin fkLogin = new FkLogin();
        fkLogin.setUname("admin");
        fkLogin.setUpwd("12345");
        //将实体(List之类的也行)转化为Json传过去
        nvps.add(new BasicNameValuePair("data", JSON.toJSONString(fkLogin)));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
          StatusLine statusLine = response.getStatusLine();
          int statusCode = statusLine.getStatusCode();
          //200为请求成功(只是请求成功,如进行查询,连上了,查询失败也是200,具体成功与否看返回的数据)
          if (statusCode == 200) {
             //获取返回值
            HttpEntity entity = response.getEntity();
            String mes = EntityUtils.toString(entity, "UTF-8");
            return mes;
          } else {
            return null;
          }
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          response.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return null;
      }
    }


    如果出现请求头不对的情况,可以把蓝色那段换成下面的

      StringEntity sentity = new StringEntity(JSON.toJSONString(fkLogin), ContentType.APPLICATION_JSON);
      httpPost.setEntity(sentity);

     需要设置header的话用httpPost.setHeader()就行了
    
    
  • 相关阅读:
    duilib listUI滚动列表的时候回出现在LIst外面显示的情况
    VS中关于字节大小的总结
    linux系统上搭建egret构建环境(针对5.3.x 版以上)
    android studio 2.0 Gradle HttpProxy 设置
    低压差稳压器--AMS1117芯片简介
    车联网技术简介
    MATLAB语音信号处理
    汇编语言系列Ⅳ 实现发出各种声音
    汇编语言系列Ⅲ 实现字符串操作
    汇编语言系列Ⅱ 实现简单数学运算
  • 原文地址:https://www.cnblogs.com/IceBlueBrother/p/8422999.html
Copyright © 2011-2022 走看看