【问题标题】:How to make a cURL command into a HTTP POST?如何将 cURL 命令转换为 HTTP POST?
【发布时间】:2018-04-14 16:19:33
【问题描述】:

谁能帮助将以下 cURL 命令转换为 HTTP POST,可以通过单击 Android 应用程序的按钮来运行?

cURL 命令:

'C:\\DevPrograms\\curl-7.59.0-win32-mingw\\bin\\curl.exe' -X POST --data-binary '@C:\xampp\htdocs\Pastec_Test_Connection\Test_Images\Test_FiveHundredEuro01.jpg' 'http://localhost:4212/index/searcher'

【问题讨论】:

标签: android curl http-post


【解决方案1】:

下面是代码:

  • 将 JPG 加载为位图。
  • 将位图转换为字节数组。
  • 通过 HttpURLConnection 使用字节数组发送 POST。
  • 从服务器获取响应。

Bitmap bitmap = null;
Uri uri = Uri.fromFile(new File("C:/xampp/htdocs/Pastec_Test_Connection/Test_Images/Test_FiveHundredEuro01.jpg"));
InputStream stream = null;

try {
    stream = getContentResolver().openInputStream(uri);
    BitmapFactory.Options options = new BitmapFactory.Options();
    bitmap = BitmapFactory.decodeStream(stream, null, options);
} catch (IOException e) {
    Log.e("IOException", e.getMessage(), e);
    return null;
} finally {
    try {
        if (stream != null) stream.close();
    } catch (Exception e) {}
}
ByteArrayOutputStream byteStream = null;
byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
// Convert ByteArrayOutputStream to byte array. Close stream. 
byte[] byteArray = byteStream.toByteArray();
byteStream.close();
byteStream = null;

URL url = null;
try {
    url = new URL("http://localhost:4212/index/searcher");
} catch (MalformedURLException e) {
    e.printStackTrace();
}
HttpURLConnection conn = null;
try {
    conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
    e.printStackTrace();
}
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
try {
    conn.setRequestMethod("POST");
} catch (ProtocolException e) {
    e.printStackTrace();
}
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(byteArray.length));
conn.setUseCaches(false);
try {
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream()) {
        wr.write(byteArray);
        wr.flush();
        wr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //Get Response  
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while ((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
    }
    rd.close();
    System.out.println(response.toString());

【讨论】:

  • 谢谢@Midhun,但HTTPClient 有问题?
  • 什么样的问题?请详细说明。
  • Cannot Resolve Symbol DefaultHttpClientCannot Resolve Symbol BasicHttpParamsCannot Resolve Symbol HttpPost 例如
  • 将此添加到您的 gradle compile 'org.apache.httpcomponents:httpclient:4.5.5'android { useLibrary 'org.apache.http.legacy' }
  • HttpClient 计划在 Android 6.0 及更高版本中删除。您是否正在为 6.0+ 构建应用程序?然后你将不得不从 HttpClient 切换到 HttpURLConnection
猜你喜欢
  • 2020-05-01
  • 1970-01-01
  • 2016-08-04
  • 2022-07-29
  • 1970-01-01
  • 2018-07-28
  • 2016-11-25
  • 2021-12-05
  • 1970-01-01
相关资源
最近更新 更多