【问题标题】:Sending String and Image to PHP script from android从android向PHP脚本发送字符串和图像
【发布时间】:2016-02-26 16:27:28
【问题描述】:

我正在使用此代码制作多部分实体并将字符串和图像发送到 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));

DefaultHttpClientHttpPost 已被弃用。

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

【问题讨论】:

    标签: android post http-post httpclient multipartentity


    【解决方案1】:

    你可以使用 URL 类:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      相关资源
      最近更新 更多