【问题标题】:Replacing PostMethod with HttpPost in Android在 Android 中用 HttpPost 替换 PostMethod
【发布时间】:2013-03-06 15:59:14
【问题描述】:

在将微博的 Android SDK 集成到应用程序中时,我发现它包含的 HttpClient 类使用了 Android 非常不喜欢的过时库(实际上我认为他们只是将 Java SDK 粘贴到 Android Eclipse 项目中并发布了它)。这个库似乎只在微博中执行一个功能,即组装 POST 请求(使用PostMethod 类)并将它们发送到微博服务器。我兴高采烈地假设用 Android 中包含的标准 Apache HttpPost 替换它会相对简单。

不幸的是,Part 类似乎没有直接的等价物。至少有一些Parts 可以替换为BasicNameValuePair 类,但是微博定义的自定义Part 看起来更像ByteArrayEntity

检查在

中称为multPartUrl(不是错字)的两个方法

Weibo's source for HttpClient

这里转载了第一个(第二个非常相似,但粘贴了不同类型的内容):

public Response multPartURL(String url,  PostParameter[] params,ImageItem item,boolean authenticated) throws WeiboException{
  PostMethod post = new PostMethod(url);
  try {
    org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
    long t = System.currentTimeMillis();
    Part[] parts=null;
    if(params==null){
      parts=new Part[1];
    }else{
      parts=new Part[params.length+1];
    }
    if (params != null ) {
      int i=0;
        for (PostParameter entry : params) {
          parts[i++]=new StringPart( entry.getName(),(String)entry.getValue());
      }
        parts[parts.length-1]=new ByteArrayPart(item.getContent(), item.getName(), item.getImageType());
      }
    post.setRequestEntity( new MultipartRequestEntity(parts, post.getParams()) );
     List<Header> headers = new ArrayList<Header>();

     if (authenticated) {
              if (basic == null && oauth == null) {
              }
              String authorization = null;
              if (null != oauth) {
                  // use OAuth
                  authorization = oauth.generateAuthorizationHeader( "POST" , url, params, oauthToken);
              } else if (null != basic) {
                  // use Basic Auth
                  authorization = this.basic;
              } else {
                  throw new IllegalStateException(
                          "Neither user ID/password combination nor OAuth consumer key/secret combination supplied");
              }
              headers.add(new Header("Authorization", authorization));
              log("Authorization: " + authorization);
          }
      client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
    client.executeMethod(post);

    Response response=new Response();
    response.setResponseAsString(post.getResponseBodyAsString());
    response.setStatusCode(post.getStatusCode());

    log("multPartURL URL:" + url + ", result:" + response + ", time:" + (System.currentTimeMillis() - t));
      return response;
  } catch (Exception ex) {
     throw new WeiboException(ex.getMessage(), ex, -1);
  } finally {
    post.releaseConnection();
  }
}

可以看出,一个MultiPartRequestEntity中添加了若干个Part,最后一个是字节数组或者文件。

  • 在最新的 Apache 库中,什么(如果有的话)等同于 MultiPartRequestEntity
  • 有没有办法将字节数组添加到UrlEncodedFormEntity
  • 是否有另一种方法可以将名称-值对添加到ByteArrayEntity
  • 还有什么我完全遗漏的吗?

【问题讨论】:

    标签: android apache weibo


    【解决方案1】:

    到目前为止我找到的最佳答案似乎是基于这个问题的答案:

    Post multipart request with Android SDK

    这显示了从哪里获得微博使用的库的非过时替代品。

    然后我换了

    Part[] parts=null;
    if(params==null){
      parts=new Part[1];
    }else{
      parts=new Part[params.length+1];
    }
    if (params != null ) {
      int i=0;
        for (PostParameter entry : params) {
          parts[i++]=new StringPart( entry.getName(),(String)entry.getValue());
      }
        parts[parts.length-1]=new ByteArrayPart(item.getContent(), item.getName(), item.getImageType());
      }
    post.setRequestEntity( new MultipartRequestEntity(parts, post.getParams()) );
    

            MultipartEntity entity = new MultipartEntity();
            if(params!=null) {
                for (PostParameter entry : params) {
                    entity.addPart(entry.getName(), new StringBody(entry.getValue()));
                }
            }
    
            entity.addPart("filename", new ByteArrayBody(item.getContent(), item.getImageType(), item.getName()));
    

    我不是 100% 确定最后一行,因为我基于微博自己的类,它设置了一个 filename 参数;但是,它很可能被称为其他东西,虽然不清楚。

    然后,我将HttpClient 替换为AndroidHttpClient,并使用标准的AndroidHttpClient 方法来获取状态代码,并使用来自实体的内容的InputStream;这个流,我传入微博Response 类并读入它作为其主要成员的字符串。这些假设可能是错误的,但我会继续研究。

    【讨论】:

      猜你喜欢
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多