2011-05-03 57 views
9

我正在通過不同的博客,網站和堆棧溢出爲我的答案。我有一個應用程序與服務器共享圖像。它們爲驗證提供了一個API令牌,並且驗證使用了curl。任何人都可以告訴我如何在我的應用程序中使用curl。我必須以相同形式傳遞url作爲API token + password+ the path才能獲得身份驗證。任何人都可以建議一些技巧來完成它。?CURL在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://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); 

這是從自身的日誌貓響應rver(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): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
05-05 09:34:31.851: INFO/server responce(7283):  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
05-05 09:34:31.851: INFO/server responce(7283): <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
05-05 09:34:31.851: INFO/server responce(7283): <head> 
05-05 09:34:31.851: INFO/server responce(7283): <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
05-05 09:34:31.851: INFO/server responce(7283): <meta name="robots" content="noindex,nofollow" /> 
05-05 09:34:31.851: INFO/server responce(7283): <title>Campfire: Sign in</title> 
05-05 09:34:31.851: INFO/server responce(7283): <link href="/stylesheets/screen.css?1304413304" media="all" rel="stylesheet" type="text/css" /> 
05-05 09:34:31.851: INFO/server responce(7283): <link href="/stylesheets/backpack-styles-for-login.css?1304413304" media="all" rel="stylesheet" type="text/css" /> 
05-05 09:34:31.851: INFO/server responce(7283): <script src="/sprockets.js?1304413286" type="text/javascript"></script> 
05-05 09:34:31.851: INFO/server responce(7283): 
05-05 09:34:31.851: INFO/server responce(7283): <meta name="queenbee-id" content="1515015" /> 
05-05 09:34:31.851: INFO/server responce(7283): <meta name="return-to-location" content="" /> 
05-05 09:34:31.851: INFO/server responce(7283): </head> 
05-05 09:34:31.851: INFO/server responce(7283): <body class="modal login "> 
05-05 09:34:31.851: INFO/server responce(7283): <div id="Container"> 
05-05 09:34:31.851: INFO/server responce(7283): 
05-05 09:34:31.851: INFO/server responce(7283): <div id="Modal">  
05-05 09:34:31.851: INFO/server responce(7283):  <div class="inner"> 
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):  <a href="http://www.campfirenow.com" class="logo_link"><img alt="Campfire Logo" height="98" id="logo" src="/images/logo.png?1304413304" width="96" /></a> 
05-05 09:34:31.851: INFO/server responce(7283):  
05-05 09:34:31.851: INFO/server responce(7283): <div id="login_dialog" class="login_dialog"> 
05-05 09:34:31.851: INFO/server responce(7283): <form action="https://launchpad.37signals.com/authenticate" method="post"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="QKfiIN4bccN8ch86vb6mcDneEec+B9iYp2/3AewQLs4=" /></div> 
05-05 09:34:31.851: INFO/server responce(7283):  <input id="product" name="product" type="hidden" value="campfire" /> 
05-05 09:34:31.851: INFO/server responce(7283):  <input id="subdomain" name="subdomain" type="hidden" value="s1" /> 
05-05 09:34:31.851: INFO/server responce(7283):  <div id="user_name_login"> 
05-05 09:34:31.851: INFO/server responce(7283):  <h2>Username</h2> 
05-05 09:34:31.851: INFO/server responce(7283):  <input autocapitalize="off" autocorrect="off" id="username" name="username" type="text" /><br/> 
05-05 09:34:31.851: INFO/server responce(7283):  <h2>Password</h2> 
05-05 09:34:31.851: INFO/server responce(7283):  <input id="password" name="password" type="password" /><br/> 
05-05 09:34:31.851: INFO/server responce(7283):  <label><input class="auto" id="remember_me" name="remember_me" type="checkbox" value="1" /> 
05-05 09:34:31.851: INFO/server responce(7283):   Remember me on this computer 
05-05 09:34:31.851: INFO/server responce(7283):  </label><br /> 
05-05 09:34:31.851: INFO/server responce(7283):  <input class="button" name="commit" type="submit" value="Sign in" /> 
05-05 09:34:31.851: INFO/server responce(7283):  <div class="extras"> 
05-05 09:34:31.851: INFO/server responce(7283):   <ul> 
05-05 09:34:31.851: INFO/server responce(7283):   <li><strong>Help:</strong> <a href="https://s1.campfirenow.com/forgot_password">I forgot my username or password</a></li> 
05-05 09:34:31.851: INFO/server responce(7283):   <li><a href="#" onclick="Login.loginWithOpenId(); return false;">Sign in using an OpenID instead</a></li> 
05-05 09:34:31.851: INFO/server responce(7283):   </ul> 
05-05 09:34:31.851: INFO/server responce(7283):  </div> 
05-05 09:34:31.851: INFO/server responce(7283):  </div> 
05-05 09:34:31.851: INFO/server responce(7283):  <div id="open_id_login" style="display: none"> 
05-05 09:34:31.851: INFO/server responce(7283):  <h2><img src="/images/openid-logo.png" alt="Sign in with an OpenID" /></h2> 
05-05 09:34:31.851: INFO/server responce(7283):  <input autocapitalize="off" autocorrect="off" id="openid_identifier" name="openid_identifier" type="text" /><br/> 
05-05 09:34:31.851: INFO/server responce(7283):  <label><input class="auto" id="remember_me" name="remember_me" type="checkbox" value="1" /> 
05-05 09:34:31.851: INFO/server responce(7283):   Remember me on this computer 
05-05 09:34:31.851: INFO/server responce(7283):  </label><br /> 
05-05 09:34:31.851: INFO/server responce(7283):  <input class="button" name="commit" type="submit" value="Sign in" /> 
05-05 09:34:31.851: INFO/server responce(7283):  <div class="extras"> 
05-05 09:34:31.851: INFO/server responce(7283):   <ul> 
05-05 09:34:31.851: INFO/server responce(7283):   <li><strong>Help:</strong> <a href="/authentication/open_id_alternative">My OpenID isn't working and I can't sign in</a></li> 
05-05 09:34:31.851: INFO/server responce(7283):   <li><a href="#" onclick="Login.loginWithUserName(); return false;">Sign in using a standard username/password</a></li> 
05-05 09:34:31.851: INFO/server responce(7283):   </ul> 
05-05 09:34:31.851: INFO/server responce(7283):  </div> 
05-05 09:34:31.851: INFO/server responce(7283):  </div> 
05-05 09:34:31.851: INFO/server responce(7283): </form> 
05-05 09:34:31.851: INFO/server responce(7283): </div> 
05-05 09:34:31.851: INFO/server responce(7283): <script type="text/javascript"> 
05-05 09:34:31.851: INFO/server responce(7283): //<![CDATA[ 
05-05 09:34:31.851: INFO/server responce(7283): Login.loginWithUserName({"animate":false}) 
05-05 09:34:31.851: INFO/server responce(7283): //]]> 
05-05 09:34:31.851: INFO/server responce(7283): </script> 
05-05 09:34:31.851: INFO/server responce(7283):  </div>  
05-05 09:34:31.851: INFO/server responce(7283): </div> 
05-05 09:34:31.851: INFO/server responce(7283): </div> 
05-05 09:34:31.851: INFO/server responce(7283): </body> 
05-05 09:34:31.851: INFO/server responce(7283): </html> 
05-05 09:34:31.921: DEBUG/dalvikvm(7283): GC_CONCURRENT freed 479K, 51% free 3158K/6343K, external 1357K/1828K, paused 6ms+9ms 
+0

如何整合捲曲在android ..share一些教程和指南..Plz .. – Nepster 2014-09-03 07:48:11

+1

@Nepster我也在尋找相同的,爲貝寶 – 2016-08-11 11:30:59

回答

6

讓我們假設你想要做以下請求:

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。

乾杯。

+0

謝謝你的帖子..但我有一個API令牌,我只是想在我的android應用程序中使用捲曲。我想訪問的網站是http://developer.37signals.com/campfire/。我發現了使用本地代碼的建議,但從未發現如何完美地使用它們,甚至沒有找到一個例子。 – rahul 2011-05-04 10:31:29

+0

我編輯了我的答案,我提供了一個例子。希望有所幫助。 – 2011-05-04 12:38:39

+0

感謝您花時間爲我..感謝的例子...我得到了一些來自服務器的響應,但我得到它作爲HTML,我期待XML或JSON。我在我的問題中添加了logcat輸出的響應,確實有這個問題發生。 – rahul 2011-05-05 12:18:08