使用 AsynTask 上传成功,但响应正文为空



我使用 AsyncTask 将图像上传到我的虚拟主机。我将代码编写为服务。图片上传成功,但仍然存在此错误: response body is null, calling onFailure(Throwable, JSONObject)

这是安卓代码:

    private void uploadToServer(Uri fileUri) {
    Log.d(TAG, "onStartUpload:" + fileUri);
    String str = getImageBase64(fileUri);
    //http://yasirameen.com/2016/05/asynchronous-http-client-android/
    RequestParams params = new RequestParams();
    params.put("encoded_string",str);
    params.put("image","newImage.png");
    AsyncHttpClient client = new AsyncHttpClient();
    //client.post(alamatServer, params, new JsonHttpResponseHandler() {
    client.post("http://testkomputer.com/androidBimbel/fcm/uploadImage.php", params, new JsonHttpResponseHandler() {
        ProgressDialog pd;
        @Override
        public void onStart() {
        }
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            //Toast.makeText(MyUploadService.this, responseString, Toast.LENGTH_SHORT).show();
            Log.d(TAG, "onFailure:" + responseString);
        }
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            //Toast.makeText(MyUploadService.this, responseString, Toast.LENGTH_LONG).show();
            String status;
            try {
                status = response.getString("success");
                Log.d("responOnSuccess", ""+status);
                if (status.equals("1")) {
                    Log.d("respon 1", ""+status);
                    // [START_EXCLUDE]
                    //broadcastPostFinished(1, xchatpos);
                    //updateChatDb(xid, 1, xchatpos);
                    taskCompleted();
                    // [END_EXCLUDE]
                } else if (status.equals("0")) {
                    // [START_EXCLUDE]
                    taskCompleted();
                    // [END_EXCLUDE]
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //Log.d(TAG, "onSuccess:" + response.getString("success"));
        }

        @Override
        public void onFinish() {
            pd.dismiss();
        }
    });
}
//Converting Selected Image to Base64Encode String
private String getImageBase64(Uri selectedImage) {
    Bitmap myImg = null;
    try {
        myImg = decodeUri(selectedImage);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // Must compress the Image to reduce image size to make upload easy
    myImg.compress(Bitmap.CompressFormat.PNG, 50, stream);
    byte[] byte_arr = stream.toByteArray();
    // Encode Image to String
    return  android.util.Base64.encodeToString(byte_arr, 0);
}
//Reducing Image Size of a selected Image
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
    // The new size we want to scale to
    final int REQUIRED_SIZE = 500;
    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE
                || height_tmp / 2 < REQUIRED_SIZE) {
            break;
        }
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }
    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
} 

这是我的 php 代码

    <?php
    header('content-type : bitmap; charset=utf-8');
   $respon = [
"success" => 0,
"pesan" => "post not found"];
    if(isset($_POST["image"])) { 
$encoded_string = $_POST["encoded_string"];
$image_name = time().$_POST["image"];
$decoded_string = base64_decode($encoded_string);
$path = 'uploads/'.$image_name;
$file = fopen($path,'w');
$is_written = fwrite($file,$decoded_string);
fclose($file);
if (file_exists($path)) {   
    $respon["success"] = 1;
        $respon["pesan"] = "success";
}else{
    $respon["success"] = 0;
        $respon["pesan"] = "fail";
}
    }echo json_encode($respon);?>

这里是日志猫

     03-21 14:42:37.798 20789-20857/com.my.chatlistview W/System.err:                   java.net.SocketTimeoutException
     03-21 14:42:37.798 20789-20857/com.my.chatlistview W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:241)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:160)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:84)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:273)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:223)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:685)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:487)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860)
     03-21 14:42:37.808 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:146)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at java.lang.Thread.run(Thread.java:841)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err: java.net.SocketTimeoutException
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
     03-21 14:42:37.818 20789-20857/com.my.chatlistview W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:241)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:160)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:84)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:273)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:223)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:685)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:487)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860)
     03-21 14:42:37.828 20789-20857/com.my.chatlistview W/System.err:     at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
     03-21 14:42:37.838 20789-20857/com.my.chatlistview W/System.err:     at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:146)
     03-21 14:42:37.838 20789-20857/com.my.chatlistview W/System.err:     at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
     03-21 14:42:37.838 20789-20857/com.my.chatlistview W/System.err:     at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
     03-21 14:42:37.838 20789-20857/com.my.chatlistview W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
     03-21 14:42:37.838 20789-20857/com.my.chatlistview W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
     03-21 14:42:37.838 20789-20857/com.my.chatlistview W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
     03-21 14:42:37.838 20789-20857/com.my.chatlistview W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
     03-21 14:42:37.838 20789-20857/com.my.chatlistview W/System.err:     at java.lang.Thread.run(Thread.java:841)
     03-21 14:42:37.838 20789-20789/com.my.chatlistview V/JsonHttpRH: response body is null, calling onFailure(Throwable, JSONObject)
     03-21 14:42:37.838 20789-20789/com.my.chatlistview W/JsonHttpRH: onFailure(int, Header[], Throwable, JSONObject) was not overriden, but callback was received
                                                                      java.net.SocketTimeoutException
                                                                 at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
                                                                 at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
                                                                 at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:241)
                                                                 at cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:160)
                                                                 at cz.msebera.android.httpclient.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:84)
                                                                 at cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:273)
                                                                 at cz.msebera.android.httpclient.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
                                                                 at cz.msebera.android.httpclient.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
                                                                 at cz.msebera.android.httpclient.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
                                                                 at cz.msebera.android.httpclient.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
                                                                 at cz.msebera.android.httpclient.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
                                                                 at cz.msebera.android.httpclient.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:223)
                                                                 at cz.msebera.android.httpclient.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
                                                                 at cz.msebera.android.httpclient.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
                                                                 at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:685)
                                                                 at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:487)
                                                                 at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860)
                                                                 at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
                                                                 at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:146)
                                                                 at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
                                                                 at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)
                                                                 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                 at java.lang.Thread.run(Thread.java:841)

任何建议都会有所帮助。

通过在 JsonHttpResponseHandler 中禁用 useRFC5179Compatibility Mode 修复了类似的错误

相关内容

最新更新