android HttpClient 附带的参数

Sending images can be done using the HttpComponents libraries. Download the latest HttpClient (currently4.0.1) binary with dependencies package and copy apache-mime4j-0.6.jar and httpmime-4.0.1.jar to your project and add them to your Java build path.

You will need to add the following imports to your class.

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

Now you can create a MultipartEntity to attach an image to your POST request. The following code shows an example of how to do this:

public void post(String url, List nameValuePairs) {
    HttpClient
httpClient = new DefaultHttpClient();
    HttpContext
localContext = new BasicHttpContext();
    HttpPost
httpPost = new HttpPost(url);

try
{
       
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

for(int index=0; index < nameValuePairs.size(); index++) {
           
if(nameValuePairs.get(index).getName().equalsIgnoreCase("image"))
{
               
// If the key equals to "image", we use FileBody to transfer the
data
               
entity.addPart(nameValuePairs.get(index).getName(), new
FileBody(new File (nameValuePairs.get(index).getValue())));
           
} else {
               
// Normal string data
               
entity.addPart(nameValuePairs.get(index).getName(), new
StringBody(nameValuePairs.get(index).getValue()));
           
}
       
}

httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost,
localContext);
    } catch
(IOException e) {
       
e.printStackTrace();
    }
}

这个方法目测写得不错,先留着用

android HttpClient 附带的参数

时间: 2024-08-29 23:34:46

android HttpClient 附带的参数的相关文章

Android HttpClient自动登陆discuz论坛!

你登陆论坛的时候,我们先看看浏览器干了什么事儿: 用Firefox打开HiPda 的登陆页面,输入用户名和密码,点登陆. 下面是通过firebug插件获取的数据: 可以看到浏览器这个http://www.hi-pda.com/forum/logging.php?action=login&loginsubmit=yes&inajax=1网址发了一个POST请求 看一下它POST的参数是什么: 可以看到一共有7个参数: 第一个cookietime=259200,这个是固定的,直接传这个值过去就

android httpClient 支持HTTPS的访问方式

项目中Android https请求地址遇到了这个异常(无终端认证): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate 是SSL协议中没有终端认证. 没有遇到过的问题,于是无奈的去找度娘....... 看了不少大神的博客后得到的解决方案如下:     /**      * Post请求连接Https服务      * @param serverURL  请求地址      * @param jsonStr    请求报文

Timeout in android httpclient

原文: http://www.cnblogs.com/codingmyworld/archive/2011/08/17/2141706.html   /* 从连接池中取连接的超时时间 */ConnManagerParams.setTimeout(params, 1000);/* 连接超时 */HttpConnectionParams.setConnectionTimeout(params, 2000);/* 请求超时 */HttpConnectionParams.setSoTimeout(par

Android 使用Sharedpreference共享参数

import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; public class MySharedpreference { private Context context; public MySharedpreference(Context context) { // TODO Auto-generated c

httpclient发送无参数的post数据

两个问题: 1.httpclient如何发送一个没有任何参数的post数据呢? 2.Web工程如何去接收一个无参数的post呢? 起因: 今天(2014.11.10)在开发中碰到了一个问题,接口提供方提供的接口是要求使用post方式发送数据的,心想这不超简单的一个东西吗?直接post过去不就是了,但是,提供的接口是没有任何参数的,不是类似这种http://api.dutycode.com/data/parm=xxx这种接口,而是http://api.dutycode.com/data.这个地址直

android httpClient 支持HTTPS的2种处理方式

摘自: http://www.kankanews.com/ICkengine/archives/9634.shtml 项目中Android https或http请求地址重定向为HTTPS的地址,相信很多人都遇到了这个异常(无终端认证): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate 1.没遇到过的问题,搜索吧,少年 log里出现这个异常,作者第一次遇到,不知道啥意思.看下字面意思,是ssl协议中没有终端认证.SSL?作

Android开发中Flag参数的讲解

Android开发中Flag参数的讲解: Intent对象在Android开发中起着举足轻重的作用,其内置了丰富的常量,用于传递数据, 下面本文将介绍跟Task有关的一些Flag参数,各参数的理解均来自Android API和本人在实际项目中的体验,如果有描述不当之处,还请各位不吝赐教. 1.FLAG_ACTIVITY_BROUGHT_TO_FRONT:不在程序代码中设置,在launchMode中设置singleTask模式时系统帮你设定. 2.FLAG_ACTIVITY_CLEAR_TOP:清

Android HttpClient 用法以及乱码解决

一.Post提交 并可以实现多文件上传 // 创建DefaultHttpClient对象 HttpClient httpclient = new DefaultHttpClient(); // 创建一个HttpGet对象 HttpPost post = new HttpPost(realUrl); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); if (params != nu

Android HttpClient HttpURLConnection相关介绍

Android HttpClient HttpURLConnection相关介绍 遇到一个问题 在android studio上用HttpClient编写网络访问代码的时候,发现该类无法导入并使用....百度了一会儿之后 发现一个强大网友已经解决了.相关博客:http://stackoverflow.com/questions/32153318/httpclient-wont-import-in-android-studio 究其原因:在Android 2.3及以上版本,使用的是HttpURLC