Android 如何使用NameValuePair发送字节HTTP?
我想使用以下NameValuePair方法从Android客户端向web服务器发送几个值:Android 如何使用NameValuePair发送字节HTTP?,android,Android,我想使用以下NameValuePair方法从Android客户端向web服务器发送几个值: public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http:/xxxxxxx"); try {
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
}
}
public void postData(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http:/xxxxxxx”);
试试{
//添加您的数据
List nameValuePairs=新的ArrayList(1);
字符串金额=paymentAmount.getText().toString();
字符串email=inputEmail.getText().toString();
添加(新的BasicNameValuePair(“捐赠金额”,金额));
添加(新的BasicNameValuePair(“email”,email));
添加(新的BasicNameValuePair(“paymentMethod”,“5”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}catch(ClientProtocolException e){
//TODO自动生成的捕捉块
}捕获(IOE){
//TODO自动生成的捕捉块
}
}
不幸的是,NameValuePair只能发送字符串,我还需要发送byte[]值。有人能帮我解决问题吗
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();
}
在此示例中,im发布图像(jpg)和字符串,您可以在此处下载多部分post库:
bm是一个位图。
您还可以使用:
Bundle bundle=new Bundle();
bundle.putString("key", "value");
byte[] b = bundle.getByteArray("key");
ByteArrayBody bab = new ByteArrayBody(b,"info");
将字节编码为字符串:字符串ba1=Base64.encodeBytes(ba)
Bitmap-bitmapOrg=BitmapFactory.decodeFile(sdPath);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
//调整图像大小
double-width=bitmapOrg.getWidth();
double height=bitmapOrg.getHeight();
双倍比=400/宽;
int newheight=(int)(比率*高度);
System.out.println(“----宽度”+宽度);
System.out.println(“----高度”+高度);
bitmapOrg=Bitmap.createScaledBitmap(bitmapOrg,400,newheight,true);
//在这里您还可以定义.PNG
compress(Bitmap.CompressFormat.JPEG,95,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encodeBytes(ba);
System.out.println(“正在上载图像----”+ba1);
字符串a=“aaaaa”;
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“图像”,ba1));
添加(新的BasicNameValuePair(“a”,a));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2:8080/upload_test/upload_image.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
//打印响应
输出=EntityUtils.toString(实体);
i(“获取响应--”,输出);
//is=entity.getContent();
Log.e(“Log_tag*******”,“良好连接”);
bitmapporg.recycle();
}捕获(例外e){
Log.e(“Log_标记*******,“http连接错误”+e.toString());
}
encodebyte[]
到Base64字符串,或者使用其他HttpEntity
例如MultipartEntity
(需要第三方库…只需谷歌一下)当我在webservice中发送Base64字符串如何处理时。我使用vb使用了asmx。这需要更改服务器端。不适合我。
Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//Resize the image
double width = bitmapOrg.getWidth();
double height = bitmapOrg.getHeight();
double ratio = 400/width;
int newheight = (int)(ratio * height);
System.out.println("———-width" + width);
System.out.println("———-height" + height);
bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);
//Here you can define .PNG as well
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeBytes(ba);
System.out.println("uploading image now ---" + ba1);
String a = "aaaaa";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", ba1));
nameValuePairs.add(new BasicNameValuePair("a", a));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:8080/upload_test/upload_image.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print responce
outPut = EntityUtils.toString(entity);
Log.i("GET RESPONSE—-", outPut);
//is = entity.getContent();
Log.e("log_tag ******", "good connection");
bitmapOrg.recycle();
}catch (Exception e) {
Log.e("log_tag ******", "Error in http connection " + e.toString());
}