Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.example.tourguide">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -16,6 +19,9 @@
<activity
android:name=".Login_Screen"
android:exported="false" />
<activity
android:name=".Country_Activity"
android:exported="false" />
<activity
android:name=".Home_Activity"
android:exported="false" />
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/example/tourguide/Country.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.tourguide;

public class Country {
private String name;


public Country(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}
76 changes: 76 additions & 0 deletions app/src/main/java/com/example/tourguide/Country_Activity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example.tourguide;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class Country_Activity extends AppCompatActivity {
ListView listView;
CustomAdapter mAdapter;
private static final String URL="https://countriesnow.space/api/v0.1/countries";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.country_name);
listView=findViewById(R.id.listView);
listView.setAdapter(mAdapter);
CountryAsyncTask task = new CountryAsyncTask();
task.execute(URL);
mAdapter = new CustomAdapter(this, new ArrayList<Country>());
listView.setAdapter(mAdapter);
}
private boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
private class CountryAsyncTask extends AsyncTask<String, Void, List<Country>> {

@Override
protected List<Country> doInBackground(String... urls) {
if (urls.length < 1 || urls[0] == null) {
return null;
}

List<Country> result = QueryUtils.fetchCountryData(urls[0]);
return result;
}

@Override
protected void onPostExecute(List<Country> data) {
boolean isConnected = isConnected();
View loadingIndicator = findViewById(R.id.loading_indicator);

mAdapter.clear();
if(isConnected) {

loadingIndicator.setVisibility(View.GONE);
mAdapter.addAll(data);
}
else{
loadingIndicator.setVisibility(View.GONE);
TextView txt=findViewById(R.id.empty_view);
txt.setText("No Internet");
}

if (data != null && !data.isEmpty()) {

}



}
}
}
52 changes: 52 additions & 0 deletions app/src/main/java/com/example/tourguide/CustomAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.tourguide;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;


public class CustomAdapter extends ArrayAdapter<Country> {


public CustomAdapter(@NonNull Context context, ArrayList<Country> arrayList) {

// pass the context and arrayList for the super
// constructor of the ArrayAdapter class
super(context, 0, arrayList);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

// convertView which is recyclable view
View currentItemView = convertView;

// of the recyclable view is null then inflate the custom layout for the same
if (currentItemView == null) {
currentItemView = LayoutInflater.from(getContext()).inflate(R.layout.customlist, parent, false);
}

// get the position of the view from the ArrayAdapter
Country country = getItem(position);
TextView magnitude = currentItemView.findViewById(R.id.country);
magnitude.setText(country.getName());
magnitude.setTextColor(Color.parseColor("#585F65"));


// then return the recyclable view
return currentItemView;
}


}

7 changes: 6 additions & 1 deletion app/src/main/java/com/example/tourguide/Home_Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
Expand All @@ -28,10 +31,12 @@ protected void onCreate(Bundle savedInstanceState) {
ivclick_hotel = findViewById(R.id.iv_home_hotel);
ivclick_travel = findViewById(R.id.iv_home_travel);


String name="";
//get the name from firebase(login)
tv_welcome.setText("Welcome, "+name);
// tv_welcome.setText("Welcome, "+name);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

}
}
172 changes: 172 additions & 0 deletions app/src/main/java/com/example/tourguide/QueryUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package com.example.tourguide;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;

/**
* Helper methods related to requesting and receiving earthquake data from USGS.
*/
public final class QueryUtils {
public static final String LOG_TAG = Country_Activity.class.getSimpleName();
private QueryUtils() {
}

/**
* Return a list of {@link Country} objects that has been built up from
* parsing a JSON response.
*/
public static ArrayList<Country> extractFromJson(String earthquakeJson) {

if(TextUtils.isEmpty(earthquakeJson)){
return null;
}
// Create an empty ArrayList that we can start adding earthquakes to
ArrayList<Country> earthquakes = new ArrayList<>();


// Try to parse the SAMPLE_JSON_RESPONSE. If there's a problem with the way the JSON
// is formatted, a JSONException exception object will be thrown.
// Catch the exception so the app doesn't crash, and print the error message to the logs.
try {
JSONObject baseJsonResponse = new JSONObject(earthquakeJson);
JSONArray earthquakeArray = baseJsonResponse.getJSONArray("data");


for(int i=0;i< earthquakeArray.length();i++) {
// Get a single earthquake at position i within the list of earthquakes
JSONObject currentCountry = earthquakeArray.getJSONObject(i);



// Extract the value for the key called "place"
String name = currentCountry.getString("country");


// Create a new {@link Earthquake} object with the magnitude, location, time,
// and url from the JSON response.
Country earthquake = new Country(name);

// Add the new {@link Earthquake} to the list of earthquakes.
earthquakes.add(earthquake);
// build up a list of Earthquake objects with the corresponding data.
}

} catch (JSONException e) {
// If an error is thrown when executing any of the above statements in the "try" block,
// catch the exception here, so the app doesn't crash. Print a log message
// with the message from the exception.
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}

// Return the list of earthquakes
return earthquakes;
}
public static ArrayList<Country> fetchCountryData(String requestUrl) {
// Create URL object
URL url = createUrl(requestUrl);

// Perform HTTP request to the URL and receive a JSON response back
String jsonResponse = null;
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
Log.e(LOG_TAG, "Problem making the HTTP request.", e);
}

// Extract relevant fields from the JSON response and create a list of {@link Earthquake}s
ArrayList<Country> earthquakes = extractFromJson(jsonResponse);

// Return the list of {@link Earthquake}s
return earthquakes;
}

/**
* Returns new URL object from the given string URL.
*/
private static URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Problem building the URL ", e);
}
return url;
}

/**
* Make an HTTP request to the given URL and return a String as the response.
*/
private static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";

// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}

HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();

// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the earthquake JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// Closing the input stream could throw an IOException, which is why
// the makeHttpRequest(URL url) method signature specifies than an IOException
// could be thrown.
inputStream.close();
}
}
return jsonResponse;
}

/**
* Convert the {@link InputStream} into a String which contains the
* whole JSON response from the server.
*/
private static String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}

}
Loading