Reputation: 2500
im lookin way to read big json content from url. Im write simple app where i test much funcitons . Most function show me time ~40-45 seconds. Than return content(JSON file realy big) Size of JSON FILE 90kb .. 2600 lines First funciton reading all content but very slow (40-45 seconds)
public String readJsonFromUrl(String urls) {
String content = "";
URL myLink = null;
String inputLine=null;
try {
myLink = new URL(urls);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
}
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader(
myLink.openStream()));
} catch (IOException e) {
}
try {
while ((inputLine = in.readLine()) != null)
content =content+inputLine;
} catch (IOException e1) {
// TODO Auto-generated catch block
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return content;
}
Function work perfect. But time :(. U guys can just input fucn to code , set url and parse.
Second function
public void readJSONFromUrl(String urls) throws IOException, URISyntaxException
{
InputStream is = null;
String response = "";
String url=urls;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
Log.i("RESPONSE","RESPONSE = "+response);
}
this funciton work but very strange. i get only PART of content and very small part of big content.
Anyone maybe know some thing better or how to fix second function for test .. it's show me how much time need for getting content. Or maybe some one have other fucntion which faste than two this function ? Regards Peter.
Upvotes: 2
Views: 1905
Reputation: 5911
It's impossible that the second function only gives you a small part of the content. The problem doesn't lie in the response or in the stream reading, it lies in you trying to display that huge content in LogCat: LogCat has a limit (see Android webservice GET request replies only a part of the XML response and What is the size limit for Logcat and how to change its capacity?; 90kb exceeds LogCat's buffer). That means your variable response does contain the full JSON, but LogCat isn't displaying the full variable. Just parse your JSON and everything should work.
Upvotes: 0
Reputation: 20500
That concatenation will take too much time appending. Use a StringBuilder instead.
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
content = response.toString();
UPDATE:
Here is an example of a getJsonObject from a url:
private static JSONObject getJSONObject(String _url) throws Exception {
if (_url.equals(""))
throw new Exception("URL can't be empty");
URL url = new URL(_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setDoInput(true);
conn.setRequestProperty("User-Agent", "android");
conn.setRequestProperty("Accept", "application/json");
conn.addRequestProperty("Content-Type", "application/json");
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
if (!url.getHost().equals(conn.getURL().getHost())) {
conn.disconnect();
return new JSONObject();
}
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
conn.disconnect();
return new JSONObject(response.toString());
}
Upvotes: 3