[Split] Using HTTP for data transfer



  • hi volker,
    I want to design a app using which user would fill in some data ,for example his user id & after he clicks "SEND" button that user id should get transfferd to a specific url(using HTTP protocol). Once it reaches to that respective url some response is sent back to the user.
    I have searched many examples but I am not able to find appropriate application that would explain how to achieve this.
    Can you guide me through this?



  • First of all:
    Please do not add completely new questions to an existing thread dealing with another topic, just open a new thread describing your problem. For this I have split off your question to a new thread.

    Regarding your question:
    Have a look at [[Doc:QNetworkAccessmanager]] and its accompanying classes. It provides what you need. Please search DevNet and the forums, countless examples have been posted already. The docs provide you some examples too, as well as the examples and demos included in the Qt sources.



  • Thank you volker for your reply.
    I couldnt start a new thread as I coudnt find a link anywhere to do so. thats the reason I posted my question on the previous thread itself.
    it would be really great if you can tell me how to start a new thread.


  • Moderators

    Go to the link "forum" on top of this page. This will open another page with all the subforums. Pick one by clicking on it.
    In the subforum you get a green button to start a new topic.

    Also "this help page":http://developer.qt.nokia.com/wiki/ForumHelp might help you.

    Cheers



  • I need my program to post some values that exist in key->value pair in my url in the log file thats stored on the server.all the examples that I see are for uploading a file or downloading a file from server.



  • You should search better. All you need is to send a POST request with your data inside in some format (e.g. JSON) and then read the reply.



  • Hii i have found a example that somehow resembles my requirements.
    but the problem now is that I am not able to include header file <QNetworkAccessManager> in my program. the application is giving error "No such File or Directory".
    what can be the actual problem?



  • hiii,
    I am trying to write a program that can send request using parametrized URL , for that purpose I am trying to include <QNetworkAccessManager.h> header file but the application is giving error as "No such file or directory".
    Am I doing some mistakes?
    How can I include this header file in my application?



  • Don't use the '.h', instead do :
    #include <QtNetwork/QNetworkManager>
    And don't forget to add

    QT += network
    

    in your .pro file.



  • try <QtNetwork/QNetworkAccessManager>. don't forget to link network module to yout project



  • I linked the network module to my .pro file nw the header file is getting included...thank u Kxyu!!

    I have written a very simple program to post parameters in 'key->value' pair using 'connect_server' slot but the values are not getting posted on the server for some strange reasons. Following is my code,can you tell me what is the mistake in it?



  • @
    #include "mynetclass.h"
    #include "ui_mynetclass.h"

    mynetclass::mynetclass(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mynetclass)
    {

    ui->setupUi(this);

    connect_server();

    }

    mynetclass::~mynetclass()
    {
    delete ui;
    }

    void mynetclass::connect_server()
    {
    QNetworkAccessManager *manager = new QNetworkAccessManager();

    QString strurl= "http://anush/alva/SlCustomObject_AndroidCalls?Param_one=Pranj& Param_two=51\\anush\nj_logs\salva";
    

    QUrl url( strurl, QUrl::TolerantMode );

    QNetworkRequest request;

    request.setUrl(url);

    manager->post(request,url.encodedQuery());
    

    }
    @

    [EDIT: code formatting, please wrap in @-tags, Volker]



  • Merged in the questions for your other thread. Please don't spread the same questions over multiple threads.

    And please use the code formatting for your source snippets, either by using the code format button in the editor, or by adding @-tags manually.



  • So how do you check if the data has been sent? if it's some kind of server-side script, I think we need its code too



  • You send the parameters twice (as parameters to the URL and as post payload).

    And now, if you did tell us what happens on the server side (or what not and what you've expected), we could be of a little bit more help.

    Please read and understand http://www.catb.org/~esr/faqs/smart-questions.html - by providing meaningful information (and not only a dump of some C++ code without comments) you're more likely to get useful answers.



  • hii kxyu!!
    As you have asked for the server-side code,This is the servlet on the server side with wich I am trying to communicate through my code.



  • @
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;

    public class SlCustomObject_AndroidCalls extends HttpServlet
    {

    public void init(ServletConfig config) throws ServletException
    {
    try
    {
    super.init(config);
    }
    catch(Exception e){Logger.toLog("Android ","SlCustomObject_AndroidCalls","init: Exception " + e);}
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    {

    try
    {

                  doPost(req,res);
    

    }

                 catch(Exception e)
    

    {

                 Logger.toLog("Android ","SlCustomObject_AndroidCalls","doGet: Exception "+e);
    

    }

    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException

    {

    try

    {

          String stParam_one="";
          String stParam_two="";
    

    if(req.getParameter("Param_one")!=null)

             {
                              
                   stParam_one = req.getParameter("Param_one").toString().trim();
       
             
             }
    

    if(req.getParameter("Param_two")!=null)

        {
                     
                  stParam_two = req.getParameter("Param_two").toString().trim();
            
        }
    

    Logger.toLog("Android ","SlCustomObject_AndroidCalls","**** Call In Servlet ******");

    Logger.toLog("Android ","SlCustomObject_AndroidCalls","stParam_one: "+stParam_one+" stParam_two: "+stParam_two);

    }

    catch(Exception e) {}

    }//end of doPost() method
    }//End of SlLogin servlet
    @



  • That's nicely formatted and indented code with a sweet bunch of empty lines that we all love to look at...

    Sorry, but do you really expect answers from that? At least a hint, what's in your sever logs would be of help.

    You've been told what to do and what to write multiple times now, so I'm out here from this point. I'm not convinced you deserve any help after being that "cooperative", sorry. I'm not after that you give me a single bit and I have to subserviently beg for another little piece of information.

    Please read and understand http://www.catb.org/~esr/faqs/smart-questions.html



  • Hello Volker...
    This is for the first time that I am communicating my queries to any expert on a forum,so please forgive me if my way of questioning have caused any inconvenience to you..
    I'll take care that I provide sufficient information before posting my queries.



  • Ok, so you're new to these kind of media, which explains things. Just some tips for the future, which make it much more likely that you get meaningful answers:

    • always remember, that the readers in a forum do know nothing about your project, so give decent background information
    • describe what you want to achieve, how you implement it and what is the result (in your case: what arrives (and what not) at the Java server side)
    • if you post code, boil it down to the absolute minimum, leave out stuff that does not "contribute" to the problem

    http://www.catb.org/~esr/faqs/smart-questions.html has some more tips for behaving "nicely".

    So, for your current problem:
    what arrives (and what not) at the Java server side?



  • Thanks for the tips I'll take care that I follow them from next time onwards...
    During this one day I was able to post my data on my server successfully..to give you a precise idea about server side,it is a simple servlet code & all that it does is checks whether there are two Parameters in the URL, and if yes then t records/writes those parameters in a text file on server.
    Till this part everything is running fine..
    But now I want to retrieve data from sever. for e.g If I have passed my user name and password as parameters can i just send the client same parameters back as a concatenated string? And then display it on my GUI? I have written the code that will send the data back to client in my servlet file,so according to me the problem is in my qt code.
    As http is asynchronous protocol I'm bit unclear about how to achieve it...can you guide me through this ?
    I hope I have provided sufficient information this time….If you want to have a look at my qt code I am copying it down for you..



  • @
    #include "mynetclass.h"#include "ui_mynetclass.h" mynetclass::mynetclass(QWidget *parent) :QMainWindow(parent),ui(new Ui::mynetclass){ui->setupUi(this);connect_server();} mynetclass::~mynetclass(){delete ui;} void mynetclass::connect_server(){QNetworkAccessManager manager = new QNetworkAccessManager(); QString strurl= "http://anush/alva/SlCustomObject_AndroidCalls?Param_one=51&Param_two =51";QUrl url( strurl, QUrl::TolerantMode ); QNetworkRequest request;request.setUrl(url);QNetworkReply reply=manager->get(request); QByteArray bytes = reply->readAll(); QString str(bytes); // string ui->resp->setText(str);}#include "mynetclass.h"
    #include "ui_mynetclass.h"

    mynetclass::mynetclass(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mynetclass)
    {
    ui->setupUi(this);
    connect_server();
    }

    mynetclass::~mynetclass()
    {
    delete ui;
    }

    void mynetclass::connect_server()
    {
    QNetworkAccessManager manager = new QNetworkAccessManager();
    QString strurl= "http://anush/alva/SlCustomObject_AndroidCalls?Param_one=pranj&Param_two =abcd";
    QUrl url( strurl, QUrl::TolerantMode );
    QNetworkRequest request;
    request.setUrl(url);
    //fetching reply from server
    QNetworkReply
    reply=manager->get(request);
    QByteArray bytes = reply->readAll();
    QString str(bytes); // string
    ui->resp->setText(str);//displaying response to the user
    }@



  • The QNetworkAccessManager classes are asynchronous. That means, the call to manager->get() returns immediately. You will have to connect to the finished signal and only handle the result at this point.

    For the data itself, it is up to you how you format that. I personally would prefer some XML for that, but something different can be ok too. A simple line separated format comes into mind. E.g. with a status code in the first line (OK, ERROR, maybe WARNING) and the payload (in case of OK, maybe split in multiple lines) or a verbose error message (in case of ERROR) afterwards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.