我使用此代码制作多部分实体并将字符串和图像发送到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 。
这里的替代代码应该是什么?
答案 0 :(得分:0)
您可以使用网址类:
URL urlobj = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) urlobj.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.connect();