从android发送字符串和图像到PHP脚本

时间:2016-02-26 16:27:28

标签: android post http-post httpclient multipartentity

我使用此代码制作多部分实体并将字符串和图像发送到PHP页面 -

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(URL);
            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
            entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            entityBuilder.addBinaryBody("Image", imageFile);              
            entityBuilder.addTextBody("name",name);

            HttpEntity entity = entityBuilder.build();
            post.setEntity(entity);
            HttpResponse response = client.execute(post);           
            HttpEntity httpEntity = response.getEntity();
            Log.d("result", EntityUtils.toString(httpEntity));

但已弃用 DefaultHttpClient HttpPost

这里的替代代码应该是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用网址类:

 URL urlobj = new URL(url);
 HttpURLConnection urlConnection = (HttpURLConnection) urlobj.openConnection();
 urlConnection.setRequestMethod("POST");
 urlConnection.connect();