×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    SOS:accept bytes of a jpg file from the J2EE,but losing some bytes.

    _LIT8(KUserAgent, "MegjoyClient 1.0");
    _LIT8(KAccept, "*/*");
    _LIT(KAcceptPackType,"Accepted :");
    _LIT(KImageLength,"The image's lenght is :");
    _LIT(KSeparator, "#");

    CMegajoyHttpEngine* CMegajoyHttpEngine::NewL(MClientObserver& aObserver)
    {
    CMegajoyHttpEngine* self = CMegajoyHttpEngine::NewLC(aObserver);
    CleanupStack::Pop(self);
    return self;
    }


    CMegajoyHttpEngine* CMegajoyHttpEngine::NewLC(MClientObserver& aObserver)
    {
    CMegajoyHttpEngine* self = new (ELeave) CMegajoyHttpEngine(aObserver);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }


    CMegajoyHttpEngine::CMegajoyHttpEngine(MClientObserver& aObserver)
    :iObserver(aObserver),
    iPostData(NULL),
    iRunning(EFalse)
    {
    }


    CMegajoyHttpEngine::~CMegajoyHttpEngine()
    {
    iSession.Close();

    delete iPostData;
    iPostData = NULL;
    }

    void CMegajoyHttpEngine::ConstructL()
    {
    // Open RHTTPSession with default protocol ("HTTP/TCP")
    TRAPD(err, iSession.OpenL());

    if(err != KErrNone) {
    // Most common error; no access point configured, and session creation leaves with KErrNotFound.
    _LIT(KErrMsg,"Cannot create session. Is internet access point configured?");
    _LIT(KExitingApp, "Exiting app.");
    CEikonEnv::Static()->InfoWinL(KErrMsg, KExitingApp);
    User::Leave(err);
    }
    }

    void CMegajoyHttpEngine::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue)
    {
    RStringF valStr = iSession.StringPool().OpenFStringL(aHdrValue);
    CleanupClosePushL(valStr);
    THTTPHdrVal val(valStr);
    aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField,RHTTPSession::GetTable()), val);
    CleanupStack::PopAndDestroy(); // valStr
    }

    void CMegajoyHttpEngine::IssueHTTPPostL(const TDesC8& aUri, const TDesC8& aContentType, const TDesC8& aBody)
    {
    // Parse string to URI
    TUriParser8 uri;
    uri.Parse(aUri);

    // Copy data to be posted into member variable; iPostData is used later in methods inherited from MHTTPDataSupplier.
    delete iPostData;
    iPostData = aBody.AllocL();

    // Get request method string for HTTP POST
    RStringF method = iSession.StringPool().StringF(HTTP::EPOST,RHTTPSession::GetTable());

    // Open transaction with previous method and parsed uri. This class will receive transaction events in MHFRunL and MHFRunError.
    iTransaction = iSession.OpenTransactionL(uri, *this, method);

    // Set headers for request; user agent, accepted content type and body's content type.
    RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
    SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
    SetHeaderL(hdr, HTTP::EAccept, KAccept);
    SetHeaderL(hdr, HTTP::EContentType, aContentType);

    // Set this class as an data supplier. Inherited MHTTPDataSupplier methods are called when framework needs to send body data.
    MHTTPDataSupplier* dataSupplier = this;
    iTransaction.Request().SetBody(*dataSupplier);

    // Submit the transaction. After this the framework will give transaction events via MHFRunL and MHFRunError.
    iTransaction.SubmitL();

    iRunning = ETrue;
    _LIT(KConnecting,"Connecting...");
    iObserver.ClientEvent(KConnecting);
    DEBUGLOG("Connecting...",0,0,0);
    }

    void CMegajoyHttpEngine::CancelTransaction()
    {
    if(!iRunning)
    return;

    // Close() also cancels transaction (Cancel() can also be used but resources allocated by transaction must be still freed with Close())
    iTransaction.Close();

    // Not running anymore
    iRunning = EFalse;
    _LIT(KTransactionCancelled, "Transaction cancelled");
    iObserver.ClientEvent(KTransactionCancelled);
    DEBUGLOG("Transaction cancelled",0,0,0);
    }


    void CMegajoyHttpEngine::MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent)
    {

    switch (aEvent.iStatus)
    {
    case THTTPEvent::EGotResponseHeaders:
    {
    RHTTPResponse resp = aTransaction.Response();
    TInt status = resp.StatusCode();

    TBuf<32> statusText;
    statusText.Copy(resp.StatusText().DesC());

    TBuf<256> text;
    _LIT(KHeaderReceived, "Header received. Status: %d %S");
    text.Format(KHeaderReceived, status, &statusText);
    iObserver.ClientEvent(text);
    }
    break;

    case THTTPEvent::EGotResponseBodyData:
    {
    MHTTPDataSupplier* body = aTransaction.Response().Body();

    // GetNextDataPart() returns ETrue, if the received part is the last one.
    TBool isLast = body->GetNextDataPart(dataChunk);
    iObserver.ClientBodyReceived(dataChunk);

    TBuf<64> text;
    _LIT(KBodyPartReceived, "%d bytes received... ");
    text.Format(KBodyPartReceived, dataChunk.Length());
    iObserver.ClientEvent(text);


    if (isLast)
    {
    _LIT(KBodyReceived,"Body received");
    iObserver.ClientEvent(KBodyReceived);
    CEikonEnv::Static()->InfoMsg(_L("the isLast is ETrue!"));
    }

    // Always remember to release the body data.
    body->ReleaseData();
    }
    break;

    case THTTPEvent::EResponseComplete:
    {
    _LIT(KTransactionComplete, "Transaction Complete");
    iObserver.ClientEvent(KTransactionComplete);
    }
    break;

    case THTTPEvent::ESucceeded:
    {
    ExternalizeConnInfoL(dataChunk);
    _LIT(KTransactionSuccessful, "Transaction Successful");
    iObserver.ClientEvent(KTransactionSuccessful);
    _LIT(KImagesName, "c:\\System\\Apps\\megajoy\\MyJpg.jpg");
    ExternalizeConnInfoL(KImagesName,dataChunk);
    HBufC* temp = HBufC::NewL(100);
    temp->Des().AppendNum(dataChunk.Length());
    iObserver.ClientEvent(*temp);
    aTransaction.Close();
    iRunning = EFalse;
    }
    break;

    case THTTPEvent::EFailed:
    {
    // Transaction completed with failure.
    _LIT(KTransactionFailed, "Transaction Failed");
    iObserver.ClientEvent(KTransactionFailed);
    aTransaction.Close();
    iRunning = EFalse;
    }
    break;

    default:

    {
    TBuf<64> text;
    if (aEvent.iStatus < 0)
    {
    _LIT(KErrorStr, "Error: %d");
    text.Format(KErrorStr, aEvent.iStatus);
    // Just close the transaction on errors
    aTransaction.Close();
    iRunning = EFalse;
    } else {
    // Other events are not errors (e.g. permanent and temporary redirections)
    _LIT(KUnrecognisedEvent, "Unrecognised event: %d");
    text.Format(KUnrecognisedEvent, aEvent.iStatus);
    }
    iObserver.ClientEvent(text);
    }
    break;
    }
    }


    TInt CMegajoyHttpEngine::MHFRunError(TInt aError, RHTTPTransaction /*aTransaction*/, const THTTPEvent& /*aEvent*/)
    {
    // Just notify about the error and return KErrNone.
    TBuf<64>text;
    _LIT(KRunError, "MHFRunError: %d");
    text.Format(KRunError, aError);
    iObserver.ClientEvent(text);
    return KErrNone;
    }


    TBool CMegajoyHttpEngine::GetNextDataPart(TPtrC8& aDataPart)
    {
    if(iPostData)
    {
    aDataPart.Set(iPostData->Des());
    }
    return ETrue;
    }



    void CMegajoyHttpEngine::ReleaseData()
    {
    // It's safe to delete iPostData now.
    delete iPostData;
    iPostData = NULL;
    }



    TInt CMegajoyHttpEngine::Reset()
    {
    return KErrNone;
    }



    TInt CMegajoyHttpEngine::OverallDataSize()
    {
    if(iPostData)
    return iPostData->Length();
    else
    return KErrNotFound ;
    }



    this Class is converted form the SDK's example named EXAMPLEHTTP.
    when the server post a bytes Stream of a jpg sized 1589 bytes.
    but the Symbian Client only accept 1266bytes.
    where is others?

    if the Client is J2ME,the application is good.

    in advanced thanks.

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    Re: SOS:accept bytes of a jpg file from the J2EE,but losing some bytes.

    why no people answer me!

Similar Threads

  1. Replies: 1
    Last Post: 2005-05-26, 14:22
  2. Loading Image data from 'raw' bytes
    By LongSteve in forum Mobile Java General
    Replies: 2
    Last Post: 2002-11-20, 17:38
  3. Replies: 0
    Last Post: 2002-11-19, 08:25
  4. jpg file size
    By moluk in forum Symbian
    Replies: 1
    Last Post: 2002-07-11, 07:18
  5. Replies: 0
    Last Post: 2002-06-10, 12:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •