Java 设置http请求的类型
如何使用org.apache.*库设置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请求 :
HttpPost hp=new HttpPost("http://server.com");
或获取
HttpGet hg=new HttpGet("http://server.com");
它很好用。但是在我的项目中,我只想对所有类型的请求使用一个函数,因为我还需要PUT和DELETE请求
那么,如何在DefaultHttpClient中设置请求类型,或者(如果不可能)如何创建PUT和DELETE请求
:
:
在常用函数中,可以使用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种不同的实现。