Android:将APIRequest更改为HttpClient

dsf9zpds  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(153)

在我的APIRequest类中

  1. if (paramBytes != null)
  2. {
  3. if(m_RequestMethod.equals("POST"))
  4. {
  5. urlConnection.setDoOutput(true);
  6. urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  7. OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
  8. out.write(paramBytes, 0, paramBytes.length); // to fix broken pipe
  9. out.flush();
  10. out.close();
  11. }
  12. }

字符串

  1. case POST:
  2. {
  3. HttpPost request = new HttpPost(url);
  4. // add headers
  5. for (NameValuePair h : headers)
  6. {
  7. // request.addHeader(h.getName(), h.getValue());
  8. request.setHeader(h.getName(), h.getValue());
  9. }
  10. // Add Parameters
  11. if (params != null && !params.isEmpty())
  12. {
  13. request.setEntity(new UrlEncodedFormEntity(params,
  14. HTTP.UTF_8));
  15. }
  16. if (entity != null)
  17. {
  18. request.setEntity(entity);
  19. }
  20. retVal = executeRequest(request, url);
  21. break;
  22. }


如何在httpclent中添加此代码

如何在HTTP客户端中传递JSON对象
在API中,有一个方法setEntity()
在HTTP中有什么方法可用吗?

wd2eg0qa

wd2eg0qa1#

创建json对象,然后调用jsonobj.put(key,value)创建一个数组列表,调用listobj.put(key,jsonob.tostring())

相关问题