【问题标题】:Upload image in json (compress in base64) from android to web service php将json中的图像(以base64压缩)从android上传到Web服务php
【发布时间】:2016-08-25 15:48:37
【问题描述】:

我正在尝试使用 PHP 将图像(以 base64 压缩绘制)作为 JSONObject 从 android 上传到 Web 服务。我尝试将 HttpURLConnection 与 post 方法一起使用。

我的代码,在 android 中。

try {

            JSONObject params = new JSONObject();
            params.put("nombre", name);
            params.put("img", imgBase64);

            Log.d("params", params.toString());


            URL url = new URL(urlServer);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            //conn.setReadTimeout(5000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");



            BufferedOutputStream BuffOut = new BufferedOutputStream(conn.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(BuffOut, "UTF-8"));
            writer.write(params.toString());
            writer.close();
            BuffOut.close();


            //open
            conn.connect();

            //do somehting with response
            int responseCode = conn.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader buffr = new BufferedReader(new
                        InputStreamReader(
                        conn.getInputStream()));

                StringBuffer sb = new StringBuffer("");
                String line = "";

                while ((line = buffr.readLine()) != null) {
                    sb.append(line);
                    break;
                }
                Log.d("leer", sb.toString());
                buffr.close();
                conn.disconnect();

                return sb.toString();

            } else {
                Log.d("err","false : " + responseCode);
                return "false : " + responseCode;
            }
        } catch (JSONException e) {
            return " Exception JSON: " + e.getMessage();
        } catch (IllegalStateException e) {
            return " IllegalStateException: " + e.getMessage();
        } catch (UnsupportedEncodingException e) {
            return " UnsupportedEncodingException: " + e.getMessage();
        } catch (ProtocolException e) {
            return " ProtocolException: " + e.getMessage();
        } catch (MalformedURLException e) {
            return " MalformedURLException: " + e.getMessage();
        } catch (IOException e) {
            return " Exception IO: " + e.getMessage();
        } catch (Exception e) {
            return " Exception: " + e.getMessage();
        }

在 Logcat 中我有一个错误,例如:ProtocolException: unexpected end of stream

在服务器端,我有 php:

header('Content-type: application/json');

if($_SERVER['REQUEST_METHOD'] == "POST"){

    if(!empty($_POST['nombre'])&&!empty($_POST['img'])){
         echo json_encode(array('status' => 'ok', 'msg' => "hi ".$_POST['nombre']. " img ".$_POST['img']));
    }
 }

【问题讨论】:

  • BufferedWriter reader ???你觉得你的名字好吗?
  • 发布我们希望看到该异常的 LogCat。
  • BufferedOutputStream inB 在 ?在吗?
  • ProtocolException ?我们看不到您使用的协议。请显示urlServer的值;
  • if(!empty($_POST['nombre'])&&!empty($_POST['img'])) 你不能在发布 json 时使用它。不是参数。这已经说过了!

标签: php android json httpurlconnection


【解决方案1】:

如果您对特定功能没有要求或有一些限制,我建议您使用 volley 进行网络处理volley home page

【讨论】:

    猜你喜欢
    • 2014-02-13
    • 2017-06-15
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2015-03-29
    • 2011-10-15
    相关资源
    最近更新 更多