call webservice using httppost method in android with parameter - android

I want to call a Web-service using a Http-post method.I want to call this URL:
http://serverurl/webservice/login.php?message=[{"username":"durgesh","password":"pass123"}]
how to call this and how to pass parameter?

I think this can help you
urlpath="http://192.168.1.158/VisionEPODWebService/VisionEPOD.apk";
String ApkName = "VisionEPOD.apk";
URL url = new URL(urlpath.toString());
// Your given URL.
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

Related

HttpUrlConnection doesn't send a request until getResponseCode is called

To put data into a database I use a GET-request. I tried to send one from within Android to my server, but the data I sent, did not appear in the database. Then I just added httpURLConnection.getResponseCode(); and everything got working fine.
So, the following is not working until the last string is uncommented
try {
URL url = new URL(urlString);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
//httpURLConnection.getResponseCode();
}
My question: Why does the request get sent only if I call getResponseCode?

url.openConnection() does not open the connection to the server

I'm going to use HttpUrlConnection for calling a WebAPI. I simply call url.OpenConnectionMethod() to open the connection, but it doesn't work. Both allowUserInteraction and client.connected values of HtpUrlConnection instance are false.
URL url = new URL("http://myserver/service/api/ads/getads");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
any help would be appreciated.
Try this:
URL url = new URL("http://yourserver/x/y/z");
URLConnection urlConnection = url.openConnection();
if this doesn't work can I see any exceptions?

problems with adding ;jsessionid in HttpURLconnection in android for GET request

In my android Application in the first screen user authenticates and servlet replies with a session id ...
in the second screen the user again calls the servlet with the session id added in the url as ;jsession="sessionid" along with the parameters...
but the servlet is not get the session id and is responding as a new session without authentication...
where is the problem?
i am using this code for the connection
URL u = new URL(aurl[0]);
url=aurl[0];
publishProgress(""+20,"Connecting...");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
publishProgress(""+40,"Connecting...");
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
publishProgress(""+45,"Connecting...");
publishProgress(""+40,aurl[0]);
int lenghtOfFile = c.getContentLength();
System.out.println(lenghtOfFile+"lenghtOfFile");
is = c.getInputStream();
I was facing the same problem and found a simple solution.
// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...
connection = new URL(url).openConnection();
// ...
connection = new URL(url).openConnection();
// ...
This is the simplest solution I found. We need to set default CookieManager.
Using java.net.URLConnection to fire and handle HTTP requests is the great blog.
Thanks
adding jseesioid in the url never worked ..
the code worked using this solution
httpsessionmanagement
especially these two...
// Gather all cookies on the first request.
URLConnection connection = new URL(url).openConnection();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
// ...
// Then use the same cookies on all subsequent requests.
connection = new URL(url).openConnection();
for (String cookie : cookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
// ...

How can we replace HttpConnection Connector.open(URL) using in J2ME into android

We have HttpConnection Connector.open(URL) in J2ME how can we replace this class when using it in android?
You can use HttpUrlConnection in android.
Example:
URL url = new URL("http://google.com");
HttpUrlConnection conn = (HttpUrlConnection)url.openConnection();

can't use Http Post method in android application

HI,
I need to use http post method for web-service in android application .But in android shows error when use this method .How to use this method in application or any other equal method for replacing this http post method.
thanks,
Lakshmanan.
URL url = new URL(my_ip_address);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("METHOD", "POST");
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//do your stuff
}

Categories

Resources