promise与HttpClient结合实现接口请求

promise.service.ts

httpGet(url, params) {
        var au=this.authenticationState
        return new Promise ((resove,reject) =>{
            this.httpClient.get(url, {
                    // @ts-ignore
                    headers: this.httpOptions.headers,
                    params
                }).subscribe((response :any)=>{
                    if(response.code===0){
                        resove(response);
                    }else{
                        reject(response);
                    }
                },(error)=>{
                    reject(error.error);
                })
          })
    }

引入promise.sevice.ts

getVFD(loading){
//ApiConstants.getVFD:url    {}:参数
        this.requestService.httpGet(ApiConstants.getVFD, {}).then((data: any) => {
            if (data.code === 0) {
                this.showConditions(data.msg,loading)
            }else if(data.code === 500){
                this.index = true
                loading.dismiss();
                this.message.info(data.msg)
            }
        }).catch(err=>{
            this.index = true
            loading.dismiss();
            this.message.info("请求数据失败")
        })
    }

ok搞定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Apache HttpClient请求POST接口的示例代码: ```java import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("http://example.com/api"); // 设置请求头 httpPost.setHeader("Content-Type", "application/json"); // 设置请求体 String requestBody = "{\"name\":\"John\",\"age\":30}"; StringEntity entity = new StringEntity(requestBody, StandardCharsets.UTF_8); httpPost.setEntity(entity); try { HttpResponse response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); String responseString = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8); System.out.println("Response: " + responseString); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们创建了一个默认的HttpClient实例,然后构造了一个HttpPost请求对象,设置了请求头和请求体,最后发送请求并获取响应。你只需要将请求地址和请求体替换成你的实际接口地址和请求参数即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值