Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 设置http请求的类型_Java_Android_Http - Fatal编程技术网

Java 设置http请求的类型

Java 设置http请求的类型,java,android,http,Java,Android,Http,如何使用org.apache.*库设置http请求的类型?例如,如果我需要发布请求: HttpPost hp=new HttpPost("http://server.com"); 或获取 HttpGet hg=new HttpGet("http://server.com"); 它很好用。但是在我的项目中,我只想对所有类型的请求使用一个函数,因为我还需要PUT和DELETE请求 那么,如何在DefaultHttpClient中设置请求类型,或者(如果不可能)如何创建PUT和DELETE请求 :

如何使用org.apache.*库设置http请求的类型?例如,如果我需要发布请求:

HttpPost hp=new HttpPost("http://server.com");
获取

HttpGet hg=new HttpGet("http://server.com");
它很好用。但是在我的项目中,我只想对所有类型的请求使用一个函数,因为我还需要PUTDELETE请求

那么,如何在DefaultHttpClient中设置请求类型,或者(如果不可能)如何创建PUTDELETE请求

:

:

在常用函数中,可以使用
httpMethod.getName()
将返回您正在执行的HTTP调用类型

PUT/DELETE方法的语法为:

  HttpMethod httpMethod = new PutMethod("http://server.com");
  HttpMethod httpMethod = new DeleteMethod("http://server.com");
在常用函数中,可以使用
httpMethod.getName()
将返回您正在执行的HTTP调用类型

PUT/DELETE方法的语法为:

  HttpMethod httpMethod = new PutMethod("http://server.com");
  HttpMethod httpMethod = new DeleteMethod("http://server.com");

PUT和DELETE请求也有类似的功能:

HttpPut hg = new HttpPut("http://server.com");
HttpDelete hg = new HttpDelete("http://server.com");

如果只需要一个函数,可以创建如下包装函数:

public HttpRequestBase httpRequest(String uri, String method) {
   switch(method) {
     case "PUT":
       return new HttpPut(uri);
     case "DELETE":
       return new HttpDelete(uri);
     case "POST":
       return new HttpPost(uri);
     case "GET":
       return new HttpGet(uri);
     default:
       return null;
   }
}

PUT和DELETE请求也有类似的功能:

HttpPut hg = new HttpPut("http://server.com");
HttpDelete hg = new HttpDelete("http://server.com");

如果只需要一个函数,可以创建如下包装函数:

public HttpRequestBase httpRequest(String uri, String method) {
   switch(method) {
     case "PUT":
       return new HttpPut(uri);
     case "DELETE":
       return new HttpDelete(uri);
     case "POST":
       return new HttpPost(uri);
     case "GET":
       return new HttpGet(uri);
     default:
       return null;
   }
}

谢谢但是如果不为每种类型的请求创建一个对象,就无法解决我的问题?通过这些方法,我将得到4种不同的实现。谢谢。但是如果不为每种类型的请求创建一个对象,就无法解决我的问题?使用这些方法,我将得到4种不同的实现。