当前位置:  开发笔记 > 编程语言 > 正文

在Android中的CURL

如何解决《在Android中的CURL》经验,为你挑选了1个好方法。

我正在通过不同的博客,网站和堆栈溢出来寻找我的答案.我有一个与服务器共享图像的应用程序.它们为身份验证提供API令牌,并且身份验证使用curl.任何人都可以告诉我如何在我的应用程序中使用curl.我必须以API token + password+ the path相同的形式传递url 才能获得身份验证.任何人都可以建议一些技巧来完成它.

提前致谢

快乐的编码


编辑

 public static String getRequest() {
        StringBuffer stringBuffer = new StringBuffer("");
        BufferedReader bufferedReader = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet();

            URI uri = new URI("http://s1.campfirenow.com/room/5e1a1/speak.xml");
            httpGet.setURI(uri);
            httpGet.addHeader(BasicScheme.authenticate(
                    new UsernamePasswordCredentials("f48be786f212f1e35dd9s265b4675358123d838e53", "X"),
                    HTTP.UTF_8, false));

            httpGet.setHeader("Content-Type", "application/xml");
            HttpResponse httpResponse = httpClient.execute(httpGet);

这是来自服务器的log cat响应(logcat输出)

05-05 09:34:29.180: INFO/global(7283): Loaded time zone names for en_US in 7701ms.
05-05 09:34:29.230: DEBUG/dalvikvm(7283): GC_CONCURRENT freed 376K, 50% free 3080K/6087K, external 1357K/1828K, paused 6ms+6ms
05-05 09:34:30.750: DEBUG/dalvikvm(7283): GC_CONCURRENT freed 687K, 52% free 3059K/6343K, external 1357K/1828K, paused 7ms+6ms
05-05 09:34:31.470: DEBUG/dalvikvm(297): GC_EXPLICIT freed 10K, 55% free 2589K/5703K, external 716K/1038K, paused 115ms
05-05 09:34:31.851: INFO/server responce(7283): 
05-05 09:34:31.851: INFO/server responce(7283): 
05-05 09:34:31.851: INFO/server responce(7283): 
05-05 09:34:31.851: INFO/server responce(7283):   
05-05 09:34:31.851: INFO/server responce(7283):   
05-05 09:34:31.851: INFO/server responce(7283):   Campfire: Sign in
05-05 09:34:31.851: INFO/server responce(7283):   
05-05 09:34:31.851: INFO/server responce(7283):   
05-05 09:34:31.851: INFO/server responce(7283):   
05-05 09:34:31.851: INFO/server responce(7283):   
05-05 09:34:31.851: INFO/server responce(7283):   
05-05 09:34:31.851: INFO/server responce(7283):   
05-05 09:34:31.851: INFO/server responce(7283): 
05-05 09:34:31.851: INFO/server responce(7283): 
05-05 09:34:31.851: INFO/server responce(7283): 
05-05 09:34:31.851: INFO/server responce(7283): 05-05 09:34:31.851: INFO/server responce(7283): 05-05 09:34:31.851: INFO/server responce(7283):
05-05 09:34:31.851: INFO/server responce(7283): 05-05 09:34:31.851: INFO/server responce(7283): 05-05 09:34:31.921: DEBUG/dalvikvm(7283): GC_CONCURRENT freed 479K, 51% free 3158K/6343K, external 1357K/1828K, paused 6ms+9ms

Xavier Ballo.. 9

我们假设您要执行以下请求:

curl -u user:password http://sample.campfirenow.com/rooms.xml

在Android中,您将执行以下操作.

public static String getRequest() {
        StringBuffer stringBuffer = new StringBuffer("");
        BufferedReader bufferedReader = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet();

            URI uri = new URI("http://sample.campfirenow.com/rooms.xml");
            httpGet.setURI(uri);
            httpGet.addHeader(BasicScheme.authenticate(
                    new UsernamePasswordCredentials("user", "password"),
                    HTTP.UTF_8, false));

            HttpResponse httpResponse = httpClient.execute(httpGet);
            InputStream inputStream = httpResponse.getEntity().getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream));

            String readLine = bufferedReader.readLine();
            while (readLine != null) {
                stringBuffer.append(readLine);
                stringBuffer.append("\n");
                readLine = bufferedReader.readLine();
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO: handle exception
                }
            }
        }
        return stringBuffer.toString();
    }

您可以根据需要访问的内容将HttpGet更改为HttpPost/HttpPut/HttpDelete.

干杯.



1> Xavier Ballo..:

我们假设您要执行以下请求:

curl -u user:password http://sample.campfirenow.com/rooms.xml

在Android中,您将执行以下操作.

public static String getRequest() {
        StringBuffer stringBuffer = new StringBuffer("");
        BufferedReader bufferedReader = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet();

            URI uri = new URI("http://sample.campfirenow.com/rooms.xml");
            httpGet.setURI(uri);
            httpGet.addHeader(BasicScheme.authenticate(
                    new UsernamePasswordCredentials("user", "password"),
                    HTTP.UTF_8, false));

            HttpResponse httpResponse = httpClient.execute(httpGet);
            InputStream inputStream = httpResponse.getEntity().getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(
                    inputStream));

            String readLine = bufferedReader.readLine();
            while (readLine != null) {
                stringBuffer.append(readLine);
                stringBuffer.append("\n");
                readLine = bufferedReader.readLine();
            }
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    // TODO: handle exception
                }
            }
        }
        return stringBuffer.toString();
    }

您可以根据需要访问的内容将HttpGet更改为HttpPost/HttpPut/HttpDelete.

干杯.

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有