简体   繁体   English

将curl命令转换为java / android

[英]translating curl command into java/android

Can someone help me figure out how to translate the following curl commands into Java syntax for use in an Android application? 有人可以帮我弄清楚如何将以下curl命令转换为Java语法,以便在Android应用程序中使用吗?

The curl commands are: curl命令是:

curl -u username:password -H "Content-Type:application/vnd.org.snia.cdmi.container" http://localhost:8080/user curl -u username:password -H“Content-Type:application / vnd.org.snia.cdmi.container” http:// localhost:8080 / user

curl -u username:password -T /home/user1/a.jpg -H "Content-Type:image" http://localhost:8080/user/a.jpg curl -u username:password -T /home/user1/a.jpg -H“Content-Type:image” http:// localhost:8080 / user / a.jpg

thanks 谢谢

You can use Apache HttpClient in Android for performing HTTP posts. 您可以在Android中使用Apache HttpClient来执行HTTP帖子。

  • HttpClient code snippet (untested) HttpClient代码段(未经测试)

     public void postData(String url) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try { // Set the headers (your -H options) httpost.setHeader("Content-type", "application/json"); // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("param1", "value1")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); } catch (Exception e) { // Exception handling } 

    } }

  • Check the following link for the basic authentication part (your -u option) 检查以下链接以获取基本身份验证部分(您的-u选项)

http://dlinsin.blogspot.com/2009/08/http-basic-authentication-with-android.html http://dlinsin.blogspot.com/2009/08/http-basic-authentication-with-android.html

  • Check the following answer for your file upload (your -T option) 检查以下答案以获取文件上传(您的-T选项)

How to upload a file using Java HttpClient library working with PHP 如何使用Java HttpClient库上传文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM