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.
Related
I'm trying to parse the json data from an url, when i try to create connection , it throws the exception as
java.net.ProtocolException: cannot write request body after response has been read
I got the response message as Not found.
and i checked the url in web browser it shows the Json data when i login wuth my credentials.
so, i found that i need to add the cookie to my connection, but i don't know how to do this.
public void parseData(String cookie){
HttpUrlConnection connection;
try{
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Cookie", cookie);
Log.e(TAG, "cookie " + cookie);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
Log.e(TAG,connection.getResponseMessage());
/**
here i'm trying to parse the data
using BufferedReader calss
**/
}
catch(IOException e){}
}
i need to add the cookie in connection.
Please help me on this.
According to this link
you can do this:
Values must be set prior to calling the connect method:
URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp");
URLConnection urlConn = myUrl.openConnection();
Create a cookie string:
String myCookie = "userId=igbrown";
Add the cookie to a request: Using the
setRequestProperty(String name, String value); method, we will add a
property named "Cookie", passing the cookie string created in the
previous step as the property value.
urlConn.setRequestProperty("Cookie", myCookie);
Send the cookie to the server: To send the cookie, simply call connect() on the URLConnection
for which we have added the cookie property:
urlConn.connect()
I'm trying to have my android app execute code where it opens a URL connection to a local php file that returns database entries in JSON format, but it's not connecting, after commenting out the other lines I can see that it throws an exception at the lines:
connection = (HttpURLConnection) url.openConnection();
connection.connect();
Heres the screen shot of android code and the error log, the returned expected json file in windows and the php file:
link
Can you change your
URL url = new URL(getListUrl);
into
URL url = new URL("http://www.google.com");
and check the incoming data.
and the following is a working example.
try {
URL url = new URL("http://www.google.com");
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException("URL Exception");
}
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
resCode = httpConn.getResponseCode();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
My argument is if this code works for remote urls, it should work with localhost as well.
Hi it must be that you are using wrong local ip address in android files
it should be 10.0.2.2 not 127.0.0.1
First tell me are you testing on emulator or device
if emulator then where you define file path it should be
http://10.0.2.2/request_entries.php
just like in code
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/request_entries.php");
Managed to get the input stream using the URL:
URL url = new URL("http://10.0.2.2/request_entries.php");
result
Thanks a bunch guys :)
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
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?
I have the following code for url connect, which throws an exception but the exception object is empty. how to connect to a url without any exception in android 2.2
HttpURLConnection urlConnection= null;
URL url = null;
try
{
url = new URL(urlString.toString());
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
}
catch(Exception ex)
{//ex is empty here}
I think a get-request does not like when output is enabled. In this case you should
not set:
urlConnection.setDoOutput(true);