Android Question need help about okhttputils upload progress

icefairy333

Active Member
Licensed User
Longtime User
please see the project,I've tested two methods of php(t.php,t2.php)
all of them are ok in the Lan,but error with WEB("Error: java.io.IOException: unexpected end of stream on Connection{app.*.com:80, proxy=DIRECT@ hostAddress=101.*.131.87 cipherSuite=none protocol=http/1.1} (recycle count=0)")

anyone can help?
the download progress is ok now,and the order is:
1.hc_ResponseSuccess
2.Response_StreamFinish
3.jobDone

the upload progress is error:
1.hc_ResponseError
2.jobdone(false)
 

Attachments

  • helpaboutuploadprogress.zip
    12.4 KB · Views: 289

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change PostFile to:
B4X:
Public Sub PostFile(Link As String, Dir As String, FileName As String)
   Dim length As Int
   If Dir = File.DirAssets Then
     Log("Cannot send files from the assets folder.")
     Return
   End If
   length = File.Size(Dir, FileName)
   Dim In As InputStream
   In = File.OpenInput(Dir, FileName)
   ocis.Initialize(In)
   req.InitializePost(Link, ocis, length)
   CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)   
End Sub
BTW, it is a mistake to set ocis and ocos to Null. It is not needed.
 
Upvote 0

icefairy333

Active Member
Licensed User
Longtime User
Change PostFile to:
B4X:
Public Sub PostFile(Link As String, Dir As String, FileName As String)
   Dim length As Int
   If Dir = File.DirAssets Then
     Log("Cannot send files from the assets folder.")
     Return
   End If
   length = File.Size(Dir, FileName)
   Dim In As InputStream
   In = File.OpenInput(Dir, FileName)
   ocis.Initialize(In)
   req.InitializePost(Link, ocis, length)
   CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)  
End Sub
BTW, it is a mistake to set ocis and ocos to Null. It is not needed.

Thank you for the reply,but this do not work,the server using php,in the php.ini the post_max_size and upload_max_filesize is set to:50M,using this code, the progress can came out,but when 100% will throw an error like:
B4X:
hc_ResponseError:java.io.IOException: unexpected end of stream on Connection{app.kaopuedu.com:80, proxy=DIRECT@ hostAddress=101.200.131.87 cipherSuite=none protocol=http/1.1} (recycle count=0) -1
JobName = job3, Success = false
Error: java.io.IOException: unexpected end of stream on Connection{app.kaopuedu.com:80, proxy=DIRECT@ hostAddress=101.200.131.87 cipherSuite=none protocol=http/1.1} (recycle count=0)
just like my old code,and I tested write a class like okhttpwrapper but modify PostPayload like:
B4X:
public void writeTo(BufferedSink sink)
          throws IOException
        {
          if (this.data != null) {
            sink.write(this.data);
          } else {
              long read;
              long total = 0;
              while ((read = source.read(sink.buffer(), SEGMENT_SIZE)) != -1) {
                    total += read;
                    sink.flush();
                    mba.raiseEventFromDifferentThread(this, this, 0, EN+"_writeprogress", false, new Object[]{total,contentLength});
                    //this.listener.transferred(total);
//                    if (mba.subExists(EN+"_writeprogress")) {
//                        mba.raiseEvent(this, EN+"_writeprogress", new Object[]{total,contentLength});
//                       
//                    }

                }
              //Util.closeQuietly(source);
          }
        }
the progress can came out either,also error when 100%(on the web),and when I upload a small file(like 440kb jpg)will be ok,I've test some kinds of network framework:OkHttp,NoHttp all of them are the same error.. where is this problem please?
 
Upvote 0

icefairy333

Active Member
Licensed User
Longtime User
Change PostFile to:
B4X:
Public Sub PostFile(Link As String, Dir As String, FileName As String)
   Dim length As Int
   If Dir = File.DirAssets Then
     Log("Cannot send files from the assets folder.")
     Return
   End If
   length = File.Size(Dir, FileName)
   Dim In As InputStream
   In = File.OpenInput(Dir, FileName)
   ocis.Initialize(In)
   req.InitializePost(Link, ocis, length)
   CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me) 
End Sub
BTW, it is a mistake to set ocis and ocos to Null. It is not needed.

can you upload to my web:http://app.kaopuedu.com/down/t3.php for test?
the php code is:
B4X:
<?php
if ($_FILES["upfile"]["error"] > 0)
  {
  echo "Error: " . $_FILES["upfile"]["error"] . "<br />";
  }
else
  {
    $newname=uniqid().".dat";
    move_uploaded_file($_FILES["upfile"]["tmp_name"],"up/".$newname);
    echo "file saved:up/".$newname;
  }
?>
send any file about 8-10 MB ,I can upload with chrome plugins(DHC) without any setting like:
QQ截图20160701165849.png


but on android, the jpg file(5.5MB) can upload, the apk file(9mb) can not upload.

on DHC no matter the file name is xxx.apk or test.bin all of them can upload ok
 
Upvote 0

icefairy333

Active Member
Licensed User
Longtime User
You are not allowed to reverse engineer our libraries without permission.

You should upload a small project with the code I posted.

sorry ~ I just using that for a test;

the demo is here:
upload is which using the code you posted(postfile method)
upload2 is using mutipart method;
all of them will take an error.
B4X:
hc_ResponseError:java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer) -1
Error: java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)
 

Attachments

  • helpaboutuploadprogress.zip
    12.6 KB · Views: 245
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've tested it with this code and it works:
B4X:
Sub Process_Globals
   Private tmr As Timer
   Private cis As CountingInputStream
End Sub

Sub Globals
   Private sUrlUpload As String="http://192.168.0.6:51042/hello"
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     tmr.Initialize("tmr", 1000)
   End If
End Sub

Sub Tmr_Tick
   If cis.IsInitialized Then
     Log(cis.Count)
   End If
End Sub

Sub Activity_Click
   Dim j As HttpJob
   j.Initialize("upload", Me)
   j.PostFile(sUrlUpload, File.DirRootExternal, "file.mp4")
   cis = j.ocis
   tmr.Enabled = True
End Sub

Sub JobDone(j As HttpJob)
   Log(j)
   j.Release
   tmr.Enabled = False
End Sub
 
Upvote 0

icefairy333

Active Member
Licensed User
Longtime User
I've tested it with this code and it works:
B4X:
Sub Process_Globals
   Private tmr As Timer
   Private cis As CountingInputStream
End Sub

Sub Globals
   Private sUrlUpload As String="http://192.168.0.6:51042/hello"
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     tmr.Initialize("tmr", 1000)
   End If
End Sub

Sub Tmr_Tick
   If cis.IsInitialized Then
     Log(cis.Count)
   End If
End Sub

Sub Activity_Click
   Dim j As HttpJob
   j.Initialize("upload", Me)
   j.PostFile(sUrlUpload, File.DirRootExternal, "file.mp4")
   cis = j.ocis
   tmr.Enabled = True
End Sub

Sub JobDone(j As HttpJob)
   Log(j)
   j.Release
   tmr.Enabled = False
End Sub

I tested with:
1.b4j server+this code works no matter in lan or web;
2.php+this code works on lan but not work on web;
3.b4j server +this code+http library(not okhttp library) works;
4.php+this code+http library(not okhttp library) works on lan and web!!!
5.4+mutipart method upload file works on lan and web;
 

Attachments

  • b4aclient_http_needapi_20.zip
    11.8 KB · Views: 269
  • b4jserver.zip
    4.6 KB · Views: 241
Upvote 0

icefairy333

Active Member
Licensed User
Longtime User
I've tested it with this code and it works:
B4X:
Sub Process_Globals
   Private tmr As Timer
   Private cis As CountingInputStream
End Sub

Sub Globals
   Private sUrlUpload As String="http://192.168.0.6:51042/hello"
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     tmr.Initialize("tmr", 1000)
   End If
End Sub

Sub Tmr_Tick
   If cis.IsInitialized Then
     Log(cis.Count)
   End If
End Sub

Sub Activity_Click
   Dim j As HttpJob
   j.Initialize("upload", Me)
   j.PostFile(sUrlUpload, File.DirRootExternal, "file.mp4")
   cis = j.ocis
   tmr.Enabled = True
End Sub

Sub JobDone(j As HttpJob)
   Log(j)
   j.Release
   tmr.Enabled = False
End Sub

please see #13 it seams okhttp library has bug?the old http library is ok.
 
Upvote 0

Kwame Twum

Active Member
Licensed User
Longtime User
I've tested it with this code and it works:
B4X:
Sub Process_Globals
   Private tmr As Timer
   Private cis As CountingInputStream
End Sub

Sub Globals
   Private sUrlUpload As String="http://192.168.0.6:51042/hello"
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     tmr.Initialize("tmr", 1000)
   End If
End Sub

Sub Tmr_Tick
   If cis.IsInitialized Then
     Log(cis.Count)
   End If
End Sub

Sub Activity_Click
   Dim j As HttpJob
   j.Initialize("upload", Me)
   j.PostFile(sUrlUpload, File.DirRootExternal, "file.mp4")
   cis = j.ocis
   tmr.Enabled = True
End Sub

Sub JobDone(j As HttpJob)
   Log(j)
   j.Release
   tmr.Enabled = False
End Sub

@Erel, I'm assuming
B4X:
cis = j.ocis
is supposed to be
B4X:
cis = j.GetInputStream     'Correct me if I'm wrong pls

I ended up with this error on that line

B4X:
java.io.FileNotFoundException: /data/data/com.me.myapp/cache: open failed: EISDIR (Is a directory)
 
Upvote 0
Top