I encountered this really strange thing while using the HttpURLConnection today. I have the below code that basically reads data from a URL, and stores the data (after parsing it to a java object using GSON) into a Java object. The data is also simultaneously stored in another object that I have serialized and saved to a file. In the event my URL is not accessible, I read the data from the file. The URL is a secure URL that I can only access over the VPN. So to test my program, I disconnected from the VPN to see if I am able to read data from the file. The first code below throws me a null pointer, while the second one doesn't. The only difference as you can see is I am using HttpURLConnection in the second example. How does using it help? Anyone encountered something similar, or am I overlooking something? ;)
Code that throws nullpointer when URL can't be accessed -
public static void getInfoFromURL() throws IOException {
Gson gson = null;
URL url = null;
BufferedReader bufferedReader = null;
String inputLine = "";
try {
gson = new Gson();
url = new URL(addressURL);
bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((inputLine = bufferedReader.readLine()) != null) {
addressData = ((AddressData) gson.fromJson(inputLine,AddressData.class));
}
} catch (MalformedURLException mue) {
logger.error("Malformed URL exception " + mue+ " occured while accessing the URL ");
} catch (IOException ioe) {
logger.error("IO exception " + ioe+ " occured while accessing the URL ");
} finally {
if (bufferedReader != null)
bufferedReader.close();
}
}
Code that works fine:
public static void getInfoFromURL() throws IOException {
Gson gson = null;
URL url = null;
BufferedReader bufferedReader = null;
HttpURLConnection connection =null;
String inputLine = "";
try {
gson = new Gson();
url = new URL(addressURL);
connection = (HttpURLConnection) url.openConnection(); //This seems to be helping
connection.connect();
bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((inputLine = bufferedReader.readLine()) != null) {
addressData = ((AddressData) gson.fromJson(inputLine,AddressData.class));
}
} catch (MalformedURLException mue) {
logger.error("Malformed URL exception " + mue+ " occured while accessing the URL ");
} catch (IOException ioe) {
logger.error("IO exception " + ioe+ " occured while accessing the URL ");
} finally {
if (bufferedReader != null)
bufferedReader.close();
}
}
Related
I am trying to parse XML code from a server to use in Android. The URL is working, and up to SB I get the XML. When converting String to InputStream i get this in the logcat : java.io.ByteArrayInputStream#9e7122d
any help ?
thanks !
private InputStream downloadUrl(String urlString) throws IOException {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(60000);
con.setReadTimeout(60000);
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
InputStream stream = IOUtils.toInputStream(sb, "UTF-8");
Log.d(TAG, "SB " + sb);
Log.d(TAG, "STREAM" + stream);
return stream;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
There is no problem here to solve. The ByteArrayInputStream#9e7122d thing is just what ByteArrayInputStream.toString() returns.
BUT Why are you doing this? Loading the entire URL into memory adds latency and wastes space, and won't fit beyond a certain size. There is no benefit. Just return con.getInputStream().
I have got problem with read output form request.
public JSONArray listLights()
{
try
{
URL adres = new URL("https://api.lifx.com/v1/lights/all");
HttpURLConnection polaczenie = (HttpURLConnection) adres.openConnection();
polaczenie.setRequestProperty("Authorization", "Bearer " + apiKey);
polaczenie.setRequestMethod("GET");
BufferedReader wejscie = new BufferedReader(new InputStreamReader((polaczenie.getInputStream())));
StringBuilder odpowiedz = new StringBuilder();
String json;
while ((json = wejscie.readLine()) != null)
odpowiedz.append(json);
wejscie.close();
return new JSONArray(odpowiedz.toString());
}
catch (Exception wyjatek)
{
wyjatek.printStackTrace();
}
return new JSONArray();
}
StackTrace
I added to AndroidManifest Internet access too.
Welcome to leave any comments. :P
EDIT:
I google internet and found partial solution. Added AsyncTask, but now I'm receiving '429' response code.
public class JSONTask extends AsyncTask<String, String, String>
{
String apiKey = "blah_blah_blah";
String txtresult;
#Override
protected String doInBackground(String... params) {
HttpsURLConnection connection = null;
BufferedReader reader = null;
try
{
URL adres = new URL(params[0]);
HttpsURLConnection polaczenie = (HttpsURLConnection) adres.openConnection();
polaczenie.setRequestProperty("Authorization", "Bearer " + apiKey);
polaczenie.setRequestMethod("GET");
System.out.println(polaczenie.getResponseCode());
InputStream stream = polaczenie.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null)
{
buffer.append(line);
}
return buffer.toString();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally {
if (connection != null)
connection.disconnect();
try
{
if (reader != null)
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
widok.setText(s);
}
}
My current StackTrace
EDIT2:
New day, new surprise. I figure out that I'm making connection with Bulb once/twice on every 10 attempts. Any ideas?
HTTP Status code 429 means too many requests in a given an amount of time. So how many requests exactly are you doing?
android.os.NetworkOnMainThreadException it means, that You have to make a htttp request from another threat than UIthread. Why are you using async task ?
Edit: You can also try make a call from postman and maybe You will see the problem.
In the end, everything is working. Problem was on the side of bulb or Lifx Cloud.
I'm trying to learn Android development by creating the movies app from the Google Udacity course. In my code below upon executing urlConnection.connect(), the code automatically goes to the finally block without any errors/exceptions.
Can you please help me see what's wrong with my code? Thanks!
public class FetchMoviesTask extends AsyncTask<Void, Void, String> {
private final String LOG_TAG = FetchMoviesTask.class.getSimpleName();
protected String doInBackground(Void... params) {
String JSONResponse = null;
//These are declared outside as they'll be used in both try and finally blocks
BufferedReader reader = null;
HttpURLConnection urlConnection = null;
try {
//construct your URL from a URI
Uri.Builder URIbuilder = new Uri.Builder();
URIbuilder.scheme("http")
.authority("api.themoviedb.org")
.appendPath("3")
.appendPath("movie")
.appendPath("popular")
.appendQueryParameter("api_key", BuildConfig.TMDB_API_KEY);
//instantiate URL
URL popularURL = new URL(URIbuilder.toString());
Log.v(LOG_TAG, "Built URL: " + popularURL.toString());
//create and open HTTP connection
urlConnection = (HttpURLConnection) popularURL.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
//InputStream is needed to read the response
//http://developer.android.com/reference/java/net/HttpURLConnection.html
InputStream inputStream = urlConnection.getInputStream();
if (inputStream == null) {
Log.e(LOG_TAG, "Null input stream");
return null; //no data returned from HTTP request
}
//!!want to see what InputStream looks like
Log.v(LOG_TAG, "inputStream.toString(): " + inputStream.toString());
//BufferedReader is used to wrap a Reader and buffer its input
//to read InputStream, a "reader" is required and that's InputStreamReader (duh)
//http://developer.android.com/reference/java/io/BufferedReader.html
reader = new BufferedReader(new InputStreamReader(inputStream));
//!!want to see what BufferedReader looks like
Log.v(LOG_TAG, "reader.toString(): " + reader.toString());
//replaced StringBuffer w/ StringBuilder. will it work?
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
builder.append(line + "\n");
}
if (builder.length() == 0) return null; //empty stream. no point in parsing
JSONResponse = builder.toString();
Log.v(LOG_TAG, "JSON Response: " + JSONResponse);
return parseJSON(JSONResponse);
} catch (IOException e) {
Log.e(LOG_TAG, "Error", e);
return null;
} catch (JSONException e) {
Log.e(LOG_TAG, "Error parsing JSON", e);
return null;
} catch (Error e) {
Log.e(LOG_TAG, "Unknown error", e);
} finally {
if (urlConnection != null) urlConnection.disconnect();
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
//will only be triggered if there's an error getting/parsing data
return null;
}
}
CommonsWare pointed me to the possible cause of the issue, which was a missing INTERNET permission. Adding it solved my problem. Thanks for all the responses!
The problem is this comment in your code:
//will only be triggered if there's an error getting/parsing data
That's false.
The return in the try block won't be ignored if a finally block is defined, only if that finally block also includes a return.
In other words, if you have "return" in both try and finally, the one inside finally is the one which gets executed.
Source: Java try-finally return design question
Edit:
You may want to check this out: Does finally always execute in Java?
Previously, i can access the string from php remotely. I find it difficult at first but AsyncTask did the work for me. Now, i can access the result of the query from php to sql server. But I would like to pass a string from my java class to php and as I googled some information, i saw some JSON post and get codes but i can't clearly understand them. Here's my code:
protected String doInBackground(Void... params) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String url = "http://122.2.8.226/MITBookstore/sqlconnect.php";
HttpURLConnection urlConnection = null;
String line;
try {
urlConnection = (HttpURLConnection) new URL(url).openConnection();
InputStream in = urlConnection.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null) {
sb.append(line);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return sb.toString();
The string is contained in "sb.toString()". Now how would I add a JSON something in my code to send string from java to php, and also get the result string from php to java as well. Thanks in advance for any help.
If you receive response as JSON format from server, make the json string to JSONObject first. And then read the json data for your use.
try {
JSONObject obj = new JSONObject(sb.toString()); // make string to json obj
Iterator iter = obj.keys(); // get all keys from json obj and iterating
while(iter.hasNext()){
String key = (String)iter.next();
String str = obj.get(key).toString();
// write your code
}
} catch(Exception e) {
e.printStackTrace();
}
Your code already contains the answer of your question. After make url connection, just add parameter for sending your data to server with OutputStreamWriter as like you did for receive the response with InpustStreamReader.
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String url = "http://122.2.8.226/MITBookstore/sqlconnect.php";
HttpURLConnection urlConnection = null;
String line;
try {
urlConnection = (HttpURLConnection) new URL(url).openConnection();
// wrtie params
OutputStreamWriter we = new OutputStreamWriter(urlConnection.getOutPutStream());
wr.write(data); // data (make json obj to 'key=value' string)
wr.flush();
wr.close();
// read response
InputStream in = urlConnection.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null) {
sb.append(line);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}enter code here
I just try to post data to google by using the following code,but always got 405 error,can anybody tell me way?
package com.tom.labs;
import java.net.*;
import java.io.*;
public class JavaHttp {
public static void main(String[] args) throws Exception {
File data = new File("D:\\in.txt");
File result = new File("D:\\out.txt");
FileOutputStream out = new FileOutputStream(result);
OutputStreamWriter writer = new OutputStreamWriter(out);
Reader reader = new InputStreamReader(new FileInputStream(data));
postData(reader,new URL("http://google.com"),writer);//Not working
//postData(reader,new URL("http://google.com/search"),writer);//Not working
sendGetRequest("http://google.com/search", "q=Hello");//Works properly
}
public static String sendGetRequest(String endpoint,
String requestParameters) {
String result = null;
if (endpoint.startsWith("http://")) {
// Send a GET request to the servlet
try {
// Send data
String urlStr = endpoint;
if (requestParameters != null && requestParameters.length() > 0) {
urlStr += "?" + requestParameters;
}
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(result);
return result;
}
/**
* Reads data from the data reader and posts it to a server via POST
* request. data - The data you want to send endpoint - The server's address
* output - writes the server's response to output
*
* #throws Exception
*/
public static void postData(Reader data, URL endpoint, Writer output)
throws Exception {
HttpURLConnection urlc = null;
try {
urlc = (HttpURLConnection) endpoint.openConnection();
try {
urlc.setRequestMethod("POST");
} catch (ProtocolException e) {
throw new Exception(
"Shouldn't happen: HttpURLConnection doesn't support POST??",
e);
}
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
OutputStream out = urlc.getOutputStream();
try {
Writer writer = new OutputStreamWriter(out, "UTF-8");
pipe(data, writer);
writer.close();
} catch (IOException e) {
throw new Exception("IOException while posting data", e);
} finally {
if (out != null)
out.close();
}
InputStream in = urlc.getInputStream();
try {
Reader reader = new InputStreamReader(in);
pipe(reader, output);
reader.close();
} catch (IOException e) {
throw new Exception("IOException while reading response", e);
} finally {
if (in != null)
in.close();
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception("Connection error (is server running at "
+ endpoint + " ?): " + e);
} finally {
if (urlc != null)
urlc.disconnect();
}
}
/**
* Pipes everything from the reader to the writer via a buffer
*/
private static void pipe(Reader reader, Writer writer) throws IOException {
char[] buf = new char[1024];
int read = 0;
while ((read = reader.read(buf)) >= 0) {
writer.write(buf, 0, read);
}
writer.flush();
}
}
405 means "method not allowed". For example, if you try to POST to a URL that doesn't allow POST, then the server will return a 405 status.
What are you trying to do by making a POST request to Google? I suspect that Google's home page only allows GET, HEAD, and maybe OPTIONS.
Here's the body of a POST request to Google, containing Google's explanation.
405. That’s an error.
The request method POST is inappropriate for the URL /. That’s all we know.