如何从YesHttp发送HTTP请求

时间:2019-11-06 15:19:50

标签: android http

这里是问题的背景,我想使用开放源代码HTTP框架发送网络请求;这是我项目中的Java库依赖项:

        <dependency>
            <groupId>com.hadoopz</groupId>
            <artifactId>YesHttp</artifactId>
            <version>1.0.43</version>
        </dependency>

我尝试了以下代码发送请求:

YesHttpEngine.getYesHttpEngine().send(request);
import com.mycomm.YesHttp.core.HttpMethod;
import com.mycomm.YesHttp.core.JsonRequest;
import com.mycomm.YesHttp.core.Request;
import com.mycomm.YesHttp.core.Response;
import com.mycomm.YesHttp.core.StringRequest;
import com.mycomm.YesHttp.core.TextBaseResponseListener;
import com.mycomm.YesHttp.core.YesHttpEngine;
import com.mycomm.YesHttp.core.YesHttpError;

我想获取常规HTTP状态代码200作为响应

2 个答案:

答案 0 :(得分:0)

如何使用YesHttp发送Http Get请求:

 Request request = new StringRequest(HttpMethod.GET, "THE_URL", new TextBaseResponseListener() {
                @Override
                public void responseMe(final String msg) {
                    Log.d("the response of success:" + msg);
                }
            },
                    new Response.ErrorListener() {
                @Override
                public void onErrorResponse(final YesHttpError error
                ) {
                    Log.e("this is the response of error:" + error.getMessage());

                }
            }, null,Request.Protocol.HTTPS_IGNORE_CERT
            ) ;
            YesHttpEngine.getYesHttpEngine().send(request);

这是使用YesHttp框架发送POST请求的方法

   Request request = new StringRequest(HttpMethod.POST, "URL", new 
         TextBaseResponseListener() {
                @Override
                public void responseMe(final String msg) {
                    Log.d("the response of success:" + msg);

                }
            },
                    new Response.ErrorListener() {
                @Override
                public void onErrorResponse(final YesHttpError error
                ) {
                    Log.e("this is the response of error:" + error.getMessage());

                }
            }, null,Request.Protocol.HTTPS_IGNORE_CERT
            ) {
                @Override
                public void getParams(Map<String, String> requestData) {
                        requestData.put("PostParameter1", "value1");
                        requestData.put("PostParameter2", "value2");
                        requestData.put("PostParameter3", "value3");
                }

            };
            YesHttpEngine.getYesHttpEngine().send(request);

这是如何使用YesHttp框架发送JSON请求

    Request request = new JsonRequest("https://www.serverURL.com/", new 
      TextBaseResponseListener() {
                @Override
                public void responseMe(final String msg) {
                    Log.d("the response>>>:" + msg);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(final YesHttpError error) {

                    Log.e(" onErrorResponse"+error);
                }
            }, null, Request.Protocol.HTTPS_IGNORE_CERT) {
            JSONObject jSONObject = new JSONObject();
                @Override
                public String JsonBodyBuilder() {
                //please add values into jSONObject before you return it
                    return jSONObject.toString();
                }
            };
            YesHttpEngine.getYesHttpEngine()
                    .send(request);

如何在YesHttp响应中处理响应标头

     Request request = new StringRequest(HttpMethod.POST,"https://www.serverURL.com/", new TextBaseResponseListener() {
                @Override
                public void responseMe(final String msg) {
                     Log.d("the response>>>:" + msg);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(final YesHttpError error) {
                   Log.e(" onErrorResponse"+error);
                }
            }, null, Request.Protocol.HTTPS_IGNORE_CERT) {

                @Override
                public void processResponseHeaders(Map<String, List<String>> headers) {
                    if (headers == null || headers.isEmpty()) {
                        return;
                    }
                    Set<String> kys = headers.keySet();
                    if (kys == null || kys.isEmpty()) {
                        return;
                    }
                    for (String key : kys) {
                        System.out.println("response header name:"+key+", value:"+ headers.get(key));
                    }
                }

            };
            YesHttpEngine.getYesHttpEngine().send(request);

如何将Http标头添加到YesHttp请求:

           Request request = new 
      StringRequest(HttpMethod.POST,"https://www.serverURL.com/", new 
       TextBaseResponseListener() {
                @Override
                public void responseMe(final String msg) {
                     Log.d("the response>>>:" + msg);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(final YesHttpError error) {
                   Log.e(" onErrorResponse"+error);
                }
            }, null, Request.Protocol.HTTPS_IGNORE_CERT) {

                @Override
                public void getHeaders(Map<String, String> headers) {
                    //please add all headers you want to :headers parameter, it is not null always, no nullpointer exception
                    headers.put("headerName","Value");
                }

            };
            YesHttpEngine.getYesHttpEngine().send(request);

如何使用YesHttp从网络下载文件

             Request request = new StringRequest(HttpMethod.GET, "https://www.server.com/files/a1c.mp4",
            new FileDownLoadResponse("c:\\downlowd\\", null, new Response.DownLoadUpLoadListener() {
                public void onProgressing(float rate) {
                    Log.d(getClass().getSimpleName(), "the download rate :" + rate);//if the value of rate is 1. it means download is done
                }
            }, null) {
        @Override
        public void responseMe(String newFileNameSimple) {
            Log.d(getClass().getSimpleName(), "the file name of newFileNameSimple:" + newFileNameSimple);
        }
    }, null, null, Request.Protocol.HTTP);
    YesHttpEngine.getYesHttpEngine().send(request);

如何使用YesHttp将文件上传到服务器

    String url = "http://www.server.com:8082/testFileUpload.xhtml";
    final File f_upload = new File("C:\\Users\\filename-5.2.0.zip");
    Request request = new FileUploadRequest(url, new TextBaseResponseListener() {
        @Override
        public void responseMe(String msg) {
            yeslog.d("FileUploadRequest.responseMe:" + msg);
        }
    }, null, new Response.ErrorListener() {
        public void onErrorResponse(YesHttpError error) {
            yeslog.e("Response.ErrorListener.onErrorResponse:" + error.getMessage());
        }

    }, yeslog, Request.Protocol.HTTP, new Response.DownLoadUpLoadListener() {
        public void onProgressing(float rate) {
            yeslog.d("UpLoad rate:" + rate);//the value of rate is 1 once upload is done
        }
    }) {
        @Override
        public void getParams(Map<String, String> requestData) {
            requestData.put("key1", "value1");
            requestData.put("key2", "value2");
            requestData.put("key3", "value3");
        }

        @Override
        public File getUploadFile() {
            return f_upload;
        }

    };
    YesHttpEngine.getYesHttpEngine().send(request);

如何在YesHttp中处理http超时

       Request request = new 
       StringRequest(HttpMethod.POST,"https://www.serverURL.com/", new TextBaseResponseListener() {
                @Override
                public void responseMe(final String msg) {
                     Log.d("the response>>>:" + msg);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(final YesHttpError error) {
                   Log.e(" onErrorResponse"+error);
                }
            }, null, Request.Protocol.HTTPS_IGNORE_CERT) {

                    @Override
                    public int getReadTimeout() {
                        return 90000;
                    }

                    @Override
                    public int getConnectTimeout() {
                        return 90000;
                    }

            };
            YesHttpEngine.getYesHttpEngine().send(request);

答案 1 :(得分:0)

您需要在Request的构造函数中添加一个侦听器

这是我的Kotlin代码:

val responseListener = Response.Listener { response, contentLength, currentRequest ->
    // Do stuff
}
val request = StringRequest("YOUR_URL", responseListener)
YesHttpEngine.getYesHttpEngine().send(request)

您还可以根据需要在请求构造函数上传递ErrorListener