please do not mark it as a duplicated because I have already searched Stackoverflow before posting it, and found this solution,
url.openstream() or urlconnection.getinputstream() raise nullpointerexception on valid URL
but this solution doesn't work either.
The problem is i am trying to openStream on this url > http://www.techworld.com/security/rss for my Android App.
but it always give NullPointerException.
I first checked the connection to see if the connection is successful,
connection.getResponseCode();
and it return 200 so the connection is ok.
Then according to the available solutions, I changed the JRE version from 1.7 to 1.6 but this still don't work.
When I try to openStream other urls then they work absolutely fine with the same code but the above link gives NPE.
this is the code,
URL mylink = new URL ("http://www.techworld.com/security/rss");
HttpURLConnection connection;
connection = (HttpURLConnection) myLink.openConnection();
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try {
domObject = new DOM(myLink.openStream());
domObject.start();
} catch (Exception e) {
Log.e(TAG,e+" " + host);
}
}
in the above code, host = myLink.getHost();.
one more thing i would like to mention that, when I run this code as a Java project instead of Android project then it loads finds and do not throw NPE.
What could be the problem here ?
This is the logCat, it only shows one line error,
01-21 20:46:11.575: E/testApp(30335): java.lang.NullPointerException www.techworld.com
At least this code worked:
URL url;
try {
url = new URL("http://www.techworld.com/security/rss");
HttpURLConnection httpURLConnection;
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
try {
String inputLine;
while ((inputLine = bufferedReader.readLine()) != null) {
stringBuffer.append(inputLine).append("\n");
}
} finally {
bufferedReader.close();
}
System.out.println(stringBuffer.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
I think your code tries to open connection twice. While the first connection is not closed, you try connect another myLink.openStream(). It may be better just using connection.getInputStream().
Related
I want to do something after timeout. but my code is not doing anything and it's just keep trying to connect...
try {
URL url = new URL(p1[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader( in ));
res = br.readLine();
}
} catch (IOException e) {
Global.mkOkDialog(c, "Couldn't connect to server.");
e.printStackTrace();
}
the message above in catch block not showing at all after 5 seconds
First of all, you don' actually connect to anything, you should call con.connect(). Catch block gets executed only in case of IOException which is not happening.
With if(con.getResponseCode()==HttpURLConnection.HTTP_OK) you're checking if the HTTP-Response-Code is 200, which it is if you get an ok-response. Any other response code is not handled by your program and thus does not execute any further code.
In java I want to read and save all the HTML from an URL(instagram), but getting Error 429 (Too many request). I think it is because I am trying to read more lines than request limits.
StringBuilder contentBuilder = new StringBuilder();
try {
URL url = new URL("https://www.instagram.com/username");
URLConnection con = url.openConnection();
InputStream is =con.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String str;
while ((str = in.readLine()) != null) {
contentBuilder.append(str);
}
in.close();
} catch (IOException e) {
log.warn("Could not connect", e);
}
String html = contentBuilder.toString();
And the Error is so;
Could not connect
java.io.IOException: Server returned HTTP response code: 429 for URL: https://www.instagram.com/username/
And it shows also that error occurs because of this line
InputStream is =con.getInputStream();
Does anybody have an idea why I get this error and/or what to do to solve it?
The problem might have been caused by the connection not being closed/disconnected.
For the input try-with-resources for automatic closing, even on exception or return is usefull too. Also you constructed an InputStreamReader that would use the default encoding of the machine where the application would run, but you need the charset of the URL's content.
readLine returns the line without line-endings (which in general is very useful). So add one.
StringBuilder contentBuilder = new StringBuilder();
try {
URL url = new URL("https://www.instagram.com/username");
URLConnection con = url.openConnection();
try (BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream(), "UTF-8"))) {
String line;
while ((line = in.readLine()) != null) {
contentBuilder.append(line).append("\r\n");
}
} finally {
con.disconnect();
} // Closes in.
} catch (IOException e) {
log.warn("Could not connect", e);
}
String html = contentBuilder.toString();
I am trying to get data from an MySQL database using a php-file. My java code is as follows:
HttpURLConnection conn = null;
URL url = null;
try {
url = new URL(getURL);
System.out.println(getURL);
conn = (HttpURLConnection)url.openConnection();
//conn.setReadTimeout(READ_TIMEOUT);
//conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput method depict handling of both send and receive
conn.setDoInput(true);
conn.setDoOutput(true);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder();
builder.appendQueryParameter("user", USER);
builder.appendQueryParameter("pass", PASS);
builder.appendQueryParameter("server", SERVER);
builder.appendQueryParameter("db", DB);
String query = builder.build().getEncodedQuery();
// Open connection for sending data
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
result = reader.readLine();
return(result);
}else{
return("unsuccessful");
}
When I go to my url (hidden in the variable getURL) using a browser, I see string of json on my screen, just as it should. However, when I output the contents of the reader (above code only takes the first line, but by adapting the code I can, of course, output more) it shows the html-code for a website displaying a 404 - Page does not exist message.
Anyone has any idea what goes wrong? Yes, I did check for typo's.
Okay, I have no clue what happened, as I didn't change anything. But all of the sudden it started working?!?
Must have been something server-side I guess...
Thanks for the input and sharing your thoughts!
I want to get the HTML code of the following Web Page (http://www.studenti.ict.uniba.it/esse3/ListaAppelliOfferta.do) after:
selecting "Dipartimento di Informatica" among Facoltà
selecting "Informatica" (or one of the others available)
clicking "Avvia Ricerca"
I am not very keen in the matter but I noticed the URL of the page stays the same after each selection!?!
Can anyone help describing, possibly in details, how can I do that? Unfortunately I am not expert in web programming.
Many thanks
After some tests, it refresh the pages with a POST request
fac_id:1012 --
cds_id:197 --
ad_id: -- Attività didattica
docente_id: -- Id of the docent selected
data:06/03/2014 -- Date
Anyway you missed the value of Attività ditattica, Docente and Data esame
Just run a HTTP request using HttpURLConnection (?) with this POST args, and with a XML parser read the output of tplmessage table.
Try this tutorial for HTTP request: click.
Try to read this to understand how to parse response: click
An example using the code of the tutorial:
HttpURLConnection connection = null;
try
{
URL url = new URL("http://www.studenti.ict.uniba.it/esse3/ListaAppelliOfferta.do");
connection = (HttpURLConnection) url.openConnection(); // open the connection with the url
String params =
"fac_id=1012&cds_id=197"; // You need to add ad_id, docente_id and data
connection.setRequestMethod("POST"); // i need to use POST request method
connection.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length)); // It will add the length of params
connection.setRequestProperty("Content-Language", "it-IT"); // language italian
connection.setUseCaches (false);
connection.setDoInput (true);
connection.setDoOutput (true);
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream ());
wr.writeBytes (params); // pass params
wr.flush (); // send request
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuilder response = new StringBuilder();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
}
catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
// close connection if created
if (connection != null)
connection.disconnect();
}
In response you will have the DOM of the page.
Anyway, use Chrome developers tool to get request args:
I have tried to fix this problem but i didn't get a good solution for this.
I just opened a socket with httsurlconnection and closed.
However, the socket i opened didn't close and the socket state was CLOSE_WAIT.
Here is my code and air log for this.
What is wrong with my code?
String https_url = "https://www.google.com";
URL url = new URL(https_url);
try {
con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("Connection", "close");
if(con!=null){
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input = br.readLine();
while(input.length() != 0)
{
input = br.readLine();
}
br.close();
con.disconnect();
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
You must surly be getting a NullPointerException from this code. You must test the result of readLine() for null before doing anything else. I don't see what the point of testing for an empty line in an HTML page is.