我必须通过 HttpPost 将图像发送到 php 服务器,但我的代码不起作用......
public void postAddToServer() {
String URL="http://xyz.com.au/abc/users/webappadpost/?userid=12531&listtype=2&listcatid=2&listsubcatid=3&listtitle=testTitle&listdesc=justDemo&listprice=1&listsuburb=adelate&listphone=9895623148&listavailable=1&listcondtn=1&listoldcondtn=3";
ByteArrayOutputStream bao = new ByteArrayOutputStream();
post_add_bitmap_image.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String baImage=Base64.encodeBytes(ba);
try {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "xxxxxxxx";
//String EndBoundary = "";
String str = twoHyphens + boundary + lineEnd;
String str4 = "Content-Disposition: form-data; name=\"image\"";
String str5 = "Content-Type: image/jpg";
String str6 = twoHyphens + boundary + twoHyphens;
String StrTotal ="\r\n" + str + str4 + "\r\n" +"\r\n"+ baImage + "\r\n" + str6;
HttpPost post = new HttpPost(URL);
post.addHeader("Content-Type","multipart/form-data;boundary="+boundary);
post.addHeader("Content-Type","image/jpg");
StringEntity se = new StringEntity(StrTotal);
se.setContentEncoding("UTF-8");
post.setEntity(se);
HttpClient client= new DefaultHttpClient();
HttpResponse response = client.execute(post);
HttpEntity getResEntity=response.getEntity();
System.out.println("RESPONSE getResEntity : "+getResEntity.toString());
String result="";
if(getResEntity!=null){
result=EntityUtils.toString(getResEntity);
System.out.println("result from server: "+result);
if(result!=null){
JSONObject object=new JSONObject(result);
statusFlag=object.getString("status");
statusFlagMessage=object.getString("message");
}
else{
System.out.println("NULL response from server.");
}
}
}
catch (RuntimeException e) {
}
catch (Exception e) {
}
}
对于 Post 方法,URL 的格式是否正确?或者它应该是这样的......URL="http://xyz.com.au/abc/users/webappadpost"
任何建议和帮助将不胜感激。