Asynctask无法使用PHP Post Image

时间:2016-07-20 16:21:33

标签: php android mysql android-asynctask

我目前正在使用Android代码上传图片,使用位图从手机上传到mysql数据库(数据类型:blob),然而,android继续发布此错误。

07-20 21:55:51.974 10963-11125/gl.helloworld.reportify D/ReportAsyncTask: Sending data[{image=/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
                                                                AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
                                                                AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCACgAPADASIA
                                                                AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
                                                                AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
                                                                ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
                                                                p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
                                                                AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
                                                                BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
                                                                U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
                                                                uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD+lm//
                                                                AGZfgP4oDNqnws8J4GOP7G0P1YenfBP07Dnd5rf/ALAP7OWpbgNM1bSsgY/sfxhrv+3n+H1H8xyw
                                                                BPyJ4H/4Ll/DDVfA7eO/E/7NvxC0jSdI8ZaH4P1f+x/GPwq8d6yde1rw/r/iHP3/AAscg6Pq+D7n
                                                                rkg9Qf8Agsx+zppnjf4JHxR4B+IfhT4M/F7wZ441j/hMvGHhA/2zomveDPETeHf+QD4ffxSNd0A4
                                                                1fkcjHh3nJyfyJeG/HeS8/8AZ2e6XXVaNOd3ay2d9Hpe2j3P0LFcZcJ41P8AtFJ35eqXNJSas2ou
                                                                1u9tdFrqjX8cf8Eh/wBnXx1k6lpek6sRj/kcPB/gbX8/eHP/ABTpAGDx7k8d6+eNQ/4IhfBzShrA
                                                                8C6V8PNJXV8f2vpGkeD9c0Aa0AWOSNB8fZ6468gnpw2f1Ksf+Cgn7CWq6n4V8MN8aNJ0rVfFui6F
                                                                rGjaNrOj66NY1z+2Sf8AhH+mg4PAU85PQZJBJ6Xwx+1x+xL8TtLx4C/an+E+rDuNH8Y6EcZZgMEt
                                                                0yM9z82DzzXvLFeMuB5l/bl7Wum7R6rTS19dt9Lcz5WwWF8Nsbe2R7NXa0stdbtt/Zi1rqrx5rOx
                                                                0f8AwTu/Z/H7Mf7NunfDAaoNX03SfiR4y1jSuACyay+kbMtjOFIJC89Twc1+NX/B1Z8KPjB8UP2R
                                                                v2U9V+DXgT4g/EHx54S+P2taoNI+D+j+INf8XroWsfCLxN5jbPD6nXT4f/tzStI3sis5I8Oq5JSM
                                                                V/QP8MNa0rxP4Lkl8B+K9J1bStJ8XRodY0cDXgANB0g9NA1/J3F+eSPu/JhSx5T9onwd4V8ceGvC
                                                                umeOvCnhTxbpWkaznRydKY9fD7g4BzjJzgDjHAxX02V5ljvqH1/H/wDI/sntq73ct3ZXbu3veV2m
                                                                0m/IxGWYN46WX4G2kpKKTesHOqo6vS7g4vq3aWrSd/41/CX7I+q+OtS/aF8L+J9V+N39ljRvG2s+
                                                                D/ht4x0bxxoPgzQte0b4hSf8T7wN/b//ABITnQ9X7ZwCeTgk+bf8E/8A9rfxV+x5+0HrX7O3x38V
                                                                /ELSPgKNZ1rP/CH/APMk/wBtEf8ACP8Aj3QtB/sDxR/bnw/8UDRiT4cHBOqrnkeNc/156D8CPhg2
                                                                7+zNK8WeEumD4P8AGHjnQsffz/yANf5zhfpxzgnPxl/wUz/4JeeGP2wf2fNJHwe1X+yP2j/hFo2u
                                                                f8Kh8Y6xq5/tjxuC03iLXvhJ458QFWzoHifW/wDkTfEQOfCPjMjIAPjM16qzXA41PLnkd7dE9Xdy
                                                                etlqm4667yjdu9zz45Fj8D/woPPdXdNPl0SlPra9nZ8q7cyTfKfyK/Ar9o34y/AvU/jf/wAFcvFG
                                                                laV8WfE/i74ya18H/h1o3xh0g67rOjeA9a/4Tv8At7xBoY0HxEDoXxA/tv4c6R8PB4d3E4YnBByf
                                                                3l+Hf/BXc+Jv+F4aV4n+APw91XVf2dBoX/CXDR/GGueBP7c/trxDr3h7QP8AhBm1/QPil/bv/IH1
                                                                fxHz/wAI13Gchc/jl/wSHsf2dPjB4l1f9gD9pzS9J8I6V8XvGX9sfDn/AITDwcTrWh/HnRW13w54
                                                                g+Enjn/iofC+v6GfFH/E4/4QzI/5HI+JOSPEVf0+Rf8ABDjwDpOqfFnVPDLeEtXPxeGiHxcf+Ew+
                                                                N/gM40Q69/YJ0KMa/wDFLQ9EI/tjVgcAgb493JDH1MUuFG5LMNdIpK0rbyve+/zvL4epx5Y88jgG
                                                                8A7RSjZyskpc1RK7va3w3a62s29Tn/2wtX/Z1P7IbeO/2xPgv4s0r4MfEbRtD/4nHw21nQ/HfjLw
                                                                Tr3jLw/J/wAI/r2hnHhbX9D8QeGNn/IxeGevHJyc/wAd0Hgf4YDxufgT468VeLPFvgHwl/bWsf8A
                                                                C/PB+jHQPiXovgIFh/wnv/CpNfP/ABPf+EY0TV9H8Qn4ddwR/wAVzlT43r+/H9sf9hjUv2m/2DPC
                                                                n7MS6tq+k6po3/CEKNX8H/2Fr+sZ8GhvDxIPj4fDDQtcyGyQw8NcAYByWr+eTxx/wQf/AGiv+El8
                                                                X+J9L1YA6xo3wr0fR9Z1j4ba5oOs6J/wrMy/8T8DwDr/AMUv7D/4ScdugAPfIPLlLwGBX/I8S23a
                                                                VtZJPfdqz0WisnHmjFHVmWHxuN5tVbRed1OS1sra7KzejaejPmX9gvQ9V+BP7cPhX4Nn4/fD34h/
                                                                D/xbjR9Z+JXg/H/CtPij/wAW+PiP4ff8h8g+BvEB1zVtHP8AwjniXP8AwiPjI/8ACC4GCT/TJrHw
                                                                A0zU9NfVD4V0nVtJyudY0f8AsLXtG6uB2z1HI69M8Dn+H7X9P+KH7MfjjV/hl8YdK1b4e/Gf4day
                                                                NY8Iax/Yx0HP/CGnQx4f17kjHGj/APCRe52g4Khj94ftNan+0V8cx+0J+1d+zH4W+N/hLUvi34M0
                                                                Lxdo2i/AjxkdEOi+PNF8PaL4c+IGg6J8KfAfiFvH+vH/AITfR1Pgwv4aJJzuY4y05nlf15bpJJLZ
                                                                20cknon7zdne103J26vsynNPqd2ttG0t07y03u7ta30au7XjzH7LftQ/Aj4OeBvhn8RPHfjrwt/Y
                                                                /hXwfo2taxq//Emz/wAgcydSRnrnufvjnpn6e/4IYfs8aX4E0n4kftPamV1Xx58cl0PSP7ZfSSqa
                                                                J4D0XxDrp/sHRmySF8Ta4oZjnppXh3g4yf5jP2kPir+098TPi78P/wBhT4m/FT43aR4W+L3xk8D6
                                                                Pq2j/En+3P7Z0TwEPEWif2Dr3/FQeHzr2uc6xjJB/wCQWOoJr+7j9jvwnp3gfwHp+k6Vpv8AY+ma
                                                                QmiaLo2k4Hy6FpAYBsYwcHJAORnI5PT5mOF/sVvtdbN73m07PV7ST6pO1m4o9zF4pY2FR2TvG2mi
                                                                ulJLpL3t7uV7SlGyfLJv63+LHwj+Hfxx8HS+CviZ4V0vxX4ZkePUZdI1cMV3xLLszhW2kB2Vm+7j
                                                                g8qSfzs8Y/8ABFr9g/xPftq+l/CrSfCnicKp/tnRtI0IcbiB9/w+B068555yQDW5/wAFgf2nfjD+
                                                                x7+wl4w+PXwJ1bSdJ8eeFPGPwt0w6trGk6NrWlLoeu/EDQ/DfiBZbfXX8k4XUhtkY+YGDg5yTX81
                                                                /wANP+DmH9uzSPCp1Dxr8K/2e/ismkZGr6
07-20 21:55:52.460 10963-10963/edu.nyp.reportify D/ReportAsyncTask: <br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: image in C:\wamp\www\safeproject\uploadReport.php on line <i>5</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0011</td><td bgcolor='#eeeeec' align='right'>241240</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\safeproject\uploadReport.php' bgcolor='#eeeeec'>..\uploadReport.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: con in C:\wamp\www\safeproject\uploadReport.php on line <i>8</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0011</td><td bgcolor='#eeeeec' align='right'>241240</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\safeproject\uploadReport.php' bgcolor='#eeeeec'>..\uploadReport.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_prepare() expects parameter 1 to be mysqli, null given in C:\wamp\www\safeproject\uploadReport.php on line <i>8</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0011</td><td bgcolor='#eeeeec' align='right'>241240</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\safeproject\uploadReport.php' bgcolor='#eeeeec'>..\uploadReport.php<b>:</b>0</td></tr><tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0857</td><td bgcolor='#eeeeec' align='right'>251704</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mysqli-prepare' target='_new'>mysqli_prepare</a>(  )</td><td title='C:\wamp\www\safeproject\uploadReport.php' bgcolor='#eeeeec'>..\uploadReport.php<b>:</b>8</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, null given in C:\wamp\www\safeproject\uploadReport.php on line <i>10</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='

这是我的php代码

<?php

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

$image = $_POST['image'];

require_once('dbConnect.php');

$sql = "INSERT INTO images (image) VALUES (?)";

$stmt = mysqli_prepare($con,$sql);

mysqli_stmt_bind_param($stmt,"s",$image);
mysqli_stmt_execute($stmt);

$check = mysqli_stmt_affected_rows($stmt);

if($check == 1){
echo "Image Uploaded Successfully";
}else{
echo "Error Uploading Image";
}
mysqli_close($con);
}else{
echo "Error";
}

这是我的ReportAsyncTask代码

public class ReportAsyncTask extends AsyncTask<Bitmap, Void, String> {
private final static String TAG = "ReportAsyncTask";
public static final String UPLOAD_URL = "http://192.168.1.8/safeproject/uploadReport.php";
public static final String UPLOAD_KEY = "image";

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : params.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

public String sendPostRequest(HashMap<String, String> postDataParams, String requestURL) {

    String result = "";
    try {
        Log.d(TAG, "Sending data[" + postDataParams + "]");
        // create HttpPost
        URL url = new URL(requestURL);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = null;

        try {
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            DataOutputStream outputStream = new DataOutputStream(urlConnection.getOutputStream());
            outputStream.flush();
            outputStream.close();
            // receive response as inputStream
            inputStream = new BufferedInputStream(urlConnection.getInputStream());
            if (inputStream != null)
                // convert inputstream to string
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";
        } finally {
            if (inputStream != null)
                inputStream.close();
            if (urlConnection != null)
                urlConnection.disconnect();
        }
    } catch (Exception e) {
        e.printStackTrace();
        result = "Connection error";
        Log.d(TAG, result);
    }
    return result;
}

private static String convertInputStreamToString(
        InputStream inputStream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while ((line = bufferedReader.readLine()) != null)
        result += line;
    inputStream.close();
    return result;
}

public String getStringImage(Bitmap bmp) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

@Override
protected String doInBackground(Bitmap... params) {
    Bitmap bitmap = params[0];
    String uploadImage = getStringImage(bitmap);

    HashMap<String, String> image = new HashMap<>();
    image.put(UPLOAD_KEY, uploadImage);

    String result = sendPostRequest(image, UPLOAD_URL);

    return result;
}

@Override
protected void onPostExecute(String response) {
    Log.d(TAG, response);
}

}

2 个答案:

答案 0 :(得分:0)

到达php的帖子不包含&#34; image&#34;。 可能超过了最大帖子大小,请查看此Increase maximum post size

答案 1 :(得分:0)

您正在记录您要发送postDataParams(并在此处发布了完整的base64编码图像。羞耻)。但是你没有将它们写入输出流。

所以你什么都没有发布。

难怪没有收到任何东西。