本文介绍了如何发送字节HTTP使用的NameValuePair?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这个的NameValuePair方法从我的Andr​​oid客户端发送了几个值到Web服务器:

 公共无效POSTDATA(){
    //创建一个新的HttpClient和门柱头球
    HttpClient的HttpClient的=新DefaultHttpClient();
    HttpPost httppost =新HttpPost(HTTP:/ XXXXXXX);

    尝试 {
        //添加数据
        名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(1);
        字符串额= paymentAmount.getText()的toString()。
        。字符串email = inputEmail.getText()的toString();
        nameValuePairs.add(新BasicNameValuePair(donationAmount,数量));
        nameValuePairs.add(新BasicNameValuePair(邮件,邮件));
        nameValuePairs.add(新BasicNameValuePair(PAYMENTMETHOD,5));
        httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));

        //执行HTTP POST请求
        HTT presponse响应= httpclient.execute(httppost);

    }赶上(ClientProtocolException E){
        // TODO自动生成的catch块
    }赶上(IOException异常E){
        // TODO自动生成的catch块
    }
}
 

不幸的NameValuePair只能够发送字符串,我需要发送byte []的值也是如此。任何人可以帮助我解决我的问题?

解决方案

  HttpPost httppost =新HttpPost(HTTP://upload-test.php);
        MultipartEntity实体=新MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        HttpClient的HttpClient的=新DefaultHttpClient();
        如果(BM!= NULL){
            ByteArrayOutputStream BOS =新ByteArrayOutputStream();
            bm.com preSS(比较pressFormat.JPEG,75,BOS);
            byte []的数据= bos.toByteArray();
            ByteArrayBody BAB =新ByteArrayBody(数据,名+JPG);
            entity.addPart(文件,BAB);
        }
        尝试 {
            StringBody SNAME =新StringBody(名称);
            entity.addPart(名,SNAME);


        }赶上(UnsupportedEncodingException E1){
            // TODO自动生成的catch块
            e1.printStackTrace();
        }

        httppost.setEntity(实体);
        尝试 {
            httpClient.execute(httppost);
        }赶上(ClientProtocolException E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
 

在这个例子中即时发布的图片(JPG格式)和字符串,你可以在这里下载多部分职位库: HTTP:// HC。 apache.org/downloads.cgi BM是一个位图。您也可以使用:

 捆绑包=新包();
bundle.putString(钥匙,价值);
byte []的B = bundle.getByteArray(钥匙);
ByteArrayBody BAB =新ByteArrayBody(二,信息);
 

I want to send a couple of values to a web server from my Android Client using this NameValuePair method:

public void postData() { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http:/xxxxxxx"); 

    try { 
        // Add your data 
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
        String amount = paymentAmount.getText().toString(); 
        String email = inputEmail.getText().toString(); 
        nameValuePairs.add(new BasicNameValuePair("donationAmount", amount)); 
        nameValuePairs.add(new BasicNameValuePair("email", email)); 
        nameValuePairs.add(new BasicNameValuePair("paymentMethod", "5")); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        // Execute HTTP Post Request 
        HttpResponse response = httpclient.execute(httppost); 

    } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block 
    } catch (IOException e) { 
        // TODO Auto-generated catch block 
    } 
}  

Unfortunately NameValuePair is only able to send String, I need to send byte[] values as well. Can anybody help me to solve my problem?

解决方案
        HttpPost httppost = new HttpPost("http://upload-test.php");
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        HttpClient httpClient = new DefaultHttpClient();
        if(bm!=null){
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bm.compress(CompressFormat.JPEG, 75, bos);
            byte[] data = bos.toByteArray();
            ByteArrayBody bab = new ByteArrayBody(data, name+".jpg");
            entity.addPart("file", bab);
        }
        try {
            StringBody sname = new StringBody(name);
            entity.addPart("name", sname);


        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        httppost.setEntity(entity);
        try {
            httpClient.execute(httppost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

In this example im posting an Image(jpg) and String you can download the multipart post library here: http://hc.apache.org/downloads.cgibm is a Bitmap.You can use also:

Bundle bundle=new Bundle();
bundle.putString("key", "value");
byte[] b = bundle.getByteArray("key");
ByteArrayBody bab = new ByteArrayBody(b,"info");

这篇关于如何发送字节HTTP使用的NameValuePair?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 19:12