[Split] Using HTTP for data transfer
-
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]
-
-
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.
-
@
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.