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

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?

Related

HttpURLConnection not allowing me to post when I have https:// in the URL

How can I get POST request to work with an "https:" url?
In the following example when I set the URL to "https" instead of "http" the HttpURLConnection automatically makes my request a "GET" ?! while when it was a regular http request it was a POST...
URL url = new URL("*https:*//www.google.com");
// URL url = new URL("*http:*//www.google.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
//writeStream(out);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
// readStream(in);
}finally {
urlConnection.disconnect();
}
enter image description here
Use HttpsURLConnection instead of HttpURLConnection

Http connection getInputStream null pointer exception android

I'm getting null pointer exception on method getInputStream() and I know that urlConnection isn't null
URL url = new URL(strUrl);
/** Creating an http connection to communcate with url */
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
/** Connecting to url */
urlConnection.connect();
/** Reading data from url */
iStream = urlConnection.getInputStream();
Url string is fine I'm checking it in my browser.
Thank you.

android urlconnection forces ssl

I am trying to connect to a server on port 8080 but AndroidHttpClient is forcing everything not on 8080 to use SSL. The offending code
URL url = new URL("http", "google.com", 8080, "");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.connect();
and this from Charles log.
note the URL field is using "https://"
now when I change the port to 80:
URL url = new URL("http", "google.com", 80, "");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.connect();
everything works fine:
I have no idea why it forces it to SSL when not on port 80. I had a similar issue with AndroidHttClient as well as DefaultHttpClient. Seems to be an issue with Android.

call webservice using httppost method in android with parameter

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();

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();

Categories

Resources