Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daniel Hartin
18,106 PointsJSON data from external website
Hi Guys
I am in the process of creating my own app which accesses data from the company website.
The problem I have is the code I have only returns the first 1644 characters the remainder are all black diamonds with question marks?. I've tried for hours now, hopefully one of you will have the answer.
package uk.co.trentbarton.trentbartontour.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.text.Html;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import javax.net.ssl.HttpsURLConnection;
import uk.co.trentbarton.trentbartontour.R;
public class RealTime {
Context mContext;
TextView resultView;
BusStop mStop;
Activity myActivity;
RelativeLayout realTimeResultsLayout;
TextView resultTypeText;
public RealTime(Context context, Activity act, BusStop stop) {
mContext = context;
myActivity = act;
resultView = (TextView) act.findViewById(R.id.realtime_result);
resultTypeText = (TextView) act.findViewById(R.id.realtime_indicator);
mStop = stop;
realTimeResultsLayout = (RelativeLayout) myActivity.findViewById(R.id.results_layout);
}
public void start(){
if (isNetworkAvailable()) {
//try to show Realtime
GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
getBlogPostsTask.execute();
} else {
//Show Scheduled time
DisplayScheduledTime();
}
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
public void handleLogResponse(JSONObject mBlogData) {
String serviceName;
String serviceDisplayName;
String dueIn;
Double lowestTime = -0.5;
String destination;
Double preciseTime;
try {
JSONArray jsonPosts = mBlogData.getJSONArray("result");
for (int i = 0; i < jsonPosts.length(); i++) {
JSONObject post = jsonPosts.getJSONObject(i);
serviceName = Html.fromHtml(post.getString(Constants.JSON_RESULT_SERVICE_NAME)).toString();
serviceDisplayName = Html.fromHtml(post.getString(Constants.JSON_RESULT_SERVICE_DISPLAY_NAME)).toString();
dueIn = Html.fromHtml(post.getString(Constants.JSON_RESULT_DUE_IN)).toString();
destination = Html.fromHtml(post.getString(Constants.JSON_RESULT_DESTINATION)).toString();
preciseTime = Double.parseDouble(Html.fromHtml(post.getString(Constants.JSON_RESULT_SERVICE_NAME)).toString());
if(serviceName.equals("6.1")){
if(lowestTime == -0.5 || preciseTime < lowestTime)
lowestTime = preciseTime;
}
}
DisplayRealTime(lowestTime);
} catch (Exception e) {
DisplayScheduledTime();
}
}
private void DisplayRealTime(Double LowestTime) {
int Mins, Secs;
Mins = (int) Math.floor(LowestTime);
Secs = (int) (LowestTime-Mins) * 60;
resultTypeText.setText("Real Time");
resultView.setText("Next bus due in " + Mins + " minutes and " + Secs + " seconds");
realTimeResultsLayout.setVisibility(View.VISIBLE);
}
private void DisplayScheduledTime() {
String timeDifference = findTimeDiff();
resultTypeText.setText("Scheduled Time");
resultView.setText(timeDifference);
realTimeResultsLayout.setVisibility(View.VISIBLE);
}
private String findTimeDiff() {
String Message = "";
Calendar c = Calendar.getInstance();
String DateString = c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date currentTime, comparisonTime;
long lowestTime = 1234567890;
for(int i = 0;i<mStop.getDepartures().size();i++){
try{
currentTime = sdf.parse(DateString);
comparisonTime = sdf.parse(mStop.getDepartures().get(i));
if((comparisonTime.getTime() - currentTime.getTime() > 0) &&
(lowestTime == 1234567890 || lowestTime > (comparisonTime.getTime() - currentTime.getTime()))){
lowestTime = comparisonTime.getTime() - currentTime.getTime();
}
}catch(ParseException e){
}
}
if (lowestTime == 1234567890){
Message = "There are no more buses today";
}else{
long seconds = lowestTime / 1000 % 60;
long mins = lowestTime / (1000*60) % 60;
long hours = lowestTime / ((1000*60)*60) % 24;
Message = "Bus due in " + hours + " hours and " + mins + " minutes.";
}
return Message;
}
private void updateDisplayForError() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Error!");
builder.setMessage("An error has occured");
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
private class GetBlogPostsTask extends AsyncTask<Object, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Object... params) {
int responseCode = -1;
JSONObject jsonResponse = null;
String message;
try {
URL realTimeUrl = new URL("https://www.trentbarton.co.uk/RTILiveTimings.aspx?m=GetRTI&service=30&stop=" + mStop.getStopCode());
HttpURLConnection connection = (HttpURLConnection) realTimeUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream,Charset.forName("iso-8859-1"));
int contentlength = connection.getContentLength();
char[] charArray = new char[contentlength];
reader.read(charArray);
String responseData = new String(charArray);
jsonResponse = new JSONObject(responseData);
} else {
//Log.i(TAG, "Unsuccessful HTTP Responses code: " + responseCode);
jsonResponse = null;
}
} catch (Exception e) {
jsonResponse = null;
}
return jsonResponse;
}
@Override
protected void onPostExecute(JSONObject result) {
handleLogResponse(result);
}
}
}
The above is my code (based very closely on the Blog Reader project here on TreeHouse, sorry if it's a bit scruffy, I haven't gone through this class and made it neater yet)
The URL is
https://www.trentbarton.co.uk/RTILiveTimings.aspx?m=GetRTI&service=30&stop=1404
I wasn't sure if the https had anything to do with it.
1 Answer
Daniel Hartin
18,106 PointsHi All
I managed to solve this by changing the request slightly and receiving the URLs HTML source code, just incase anyone has this issue the code I used in my try/catch block of the GetBlogPostTask is below
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("https://www.trentbarton.co.uk/RTILiveTimings.aspx?m=GetRTI&service=30&stop=" + mStop.getStopCode());
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
html = str.toString();
jsonResponse = new JSONObject(html);