【问题标题】:Android : change APIRequest to HttpClientAndroid:将 APIRequest 更改为 HttpClient
【发布时间】:2015-11-02 09:59:31
【问题描述】:

在我的 APIRequest 类中

if (paramBytes != null)
            {
                if(m_RequestMethod.equals("POST"))
                {
                    urlConnection.setDoOutput(true);
                    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
                    out.write(paramBytes, 0, paramBytes.length); // to fix broken pipe
                    out.flush();
                    out.close();
                }   

            }

case POST:
        {
            HttpPost request = new HttpPost(url);

            // add headers
            for (NameValuePair h : headers)
            {
                // request.addHeader(h.getName(), h.getValue());
                request.setHeader(h.getName(), h.getValue());
            }

            // Add Parameters
            if (params != null && !params.isEmpty())
            {
                request.setEntity(new UrlEncodedFormEntity(params,
                        HTTP.UTF_8));
            }

            if (entity != null)
            {

                request.setEntity(entity);
            }

            retVal = executeRequest(request, url);

            break;
        }

如何在 httpcilent 中添加此代码 要么 如何在http客户端中传递一个jsonobject 在 api 中有一个方法 setEntity() 在 http 中是否有可用的方法?

【问题讨论】:

  • 你为什么不使用截击?现在有点正式了
  • http 已弃用,使用 volley 或改造等

标签: android api httpclient


【解决方案1】:

创建 json 对象然后调用 jsonobj.put(key,value) 创建一个namevaluepair的arraylist call listobj.put(key, jsonob.tostring())

【讨论】:

  • 我正在使用 eclips,它是维护的一部分。
  • 您能否详细说明您的最后一个答案。我没有得到你。是否意味着您想提高应用性能。?
  • 不,实际上我已经为这个应用程序使用了 http 客户端,我无法将整个应用程序请求更改为 volley
  • bt http 客户端已被弃用,它可能会在以后的版本中被删除,所以如果你将代码转换为 volley 或改造会更好
  • 好的,让我试试,但它很头疼。传输整个应用请求。
猜你喜欢
  • 2018-11-03
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
相关资源
最近更新 更多