提问者:小点点

我必须把什么作为我的HttpGet方法的uri?


我正在做一个API的REST应用程序在Android和我试图让我的GETPOSTPUTDELETE方法从我的应用程序在Android。

我看到对于这个目标,我必须使用HttpGet(String uri)方法,但我对我必须放入的uri并不真正安全。我在这里看到了信息:

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpGet.html#HttpGet(java.netURI)

我有没有把我的api. php文件分配的文件夹的uri?api.php它是我有GET、PUT、POST和DELETE方法的文件。

我是否将我在方法(GET/POST/PUT/DELETE)上设置的uri用于引用它们检索给我的信息?

我在网上搜索,但我找不到任何对我有帮助的。

提前感谢!


共2个答案

匿名用户

在您的情况下,URI可能是REST Web服务endpoint的URL(通常与URI互换使用)(例如超文本传输协议://soleserver/restService/用户)。

构造函数HttpGet(String uri)接受String作为参数,因此您可以将URL用双引号括起来:

HttpGet httpGet = new HttpGet("http://someserver/restService/users");

匿名用户

这里有一些解释,因为我的评论没有真正的信息):

根据http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html路径可以是:

获取http://www.w3.org/pub/WWW/TheProject.htmlHTTP/1.1

它在http组件-客户端中工作得很好。第2个选项是:

获取 /pub/WWW/TheProject.htmlHTTP/1.1主机:www.w3.org

如果您设置Host Header应该没问题,但由于某种原因http组件-客户端只会抛出org. apache.http.client.ClientStatcolException,所以您需要设置默认主机名,下面是这两个选项的快速代码示例。可能我缺少一些设置默认主机名的明显方法。但最简单的方法是使用文件的完整路径。

import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.DefaultRoutePlanner;
import org.apache.http.impl.conn.DefaultSchemePortResolver;
import org.apache.http.protocol.HttpContext;


public class Main {

    public static void main(String[] args) throws Exception {
        // this works fine
        testAbsolutePath(); 

        // this throws org.apache.http.client.ClientProtocolException
        // if you don't specify target Host. 
        // i'm setting HttpRoutePlanner on HttpClient in this example to do this
        testRelativePath(); 

    }
    public static void testAbsolutePath() throws Exception{
        HttpClient client = HttpClientBuilder.create().build();
        String absolutePath = "http://stackoverflow.com/questions/30605198/what-i-have-to-put-as-the-uri-of-my-httpget-method";
        HttpGet absolutePathRequest = new HttpGet(absolutePath);
        testRequest(absolutePathRequest, client);
    }

    public static void testRelativePath() throws Exception{
        // the easiest way i could find to set default hostname for     
        // 'org.apache.httpcomponents:httpclient:4.3.6'
        HttpRoutePlanner rp = new DefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE) {
            @Override
            public HttpRoute determineRoute(final HttpHost host,final HttpRequest request,final HttpContext context) throws HttpException {
                HttpHost target = host != null ? host : new HttpHost("www.stackoverflow.com");
                return super.determineRoute(target, request, context);
            }
        };

        HttpClient client =  HttpClientBuilder.create().setRoutePlanner(rp).build();
        String relativePath = "/questions/30605198/what-i-have-to-put-as-the-uri-of-my-httpget-method";
        HttpGet relativePathRequest = new HttpGet(relativePath);

        testRequest(relativePathRequest, client);
    }

    public static void testRequest(HttpGet request, HttpClient client) throws Exception {
        System.out.println(request);
        for (Header h : request.getAllHeaders()) System.out.println(h);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = client.execute(request, responseHandler);
        System.out.println(responseBody.substring(0, 100)); //substring because don't want to print all page
    }
}