|
package mohammad.samandari.whowroteit; |
|
|
|
import androidx.appcompat.app.AppCompatActivity; |
|
|
|
import android.content.Context; |
|
import android.hardware.input.InputManager; |
|
import android.net.ConnectivityManager; |
|
import android.net.NetworkInfo; |
|
import android.net.Uri; |
|
import android.os.Build; |
|
import android.os.Bundle; |
|
import android.util.Log; |
|
import android.view.View; |
|
import android.view.inputmethod.InputMethodManager; |
|
import android.widget.EditText; |
|
import android.widget.TextView; |
|
|
|
import com.android.volley.Request; |
|
import com.android.volley.RequestQueue; |
|
import com.android.volley.Response; |
|
import com.android.volley.VolleyError; |
|
import com.android.volley.toolbox.StringRequest; |
|
import com.android.volley.toolbox.Volley; |
|
|
|
import org.json.JSONArray; |
|
import org.json.JSONException; |
|
import org.json.JSONObject; |
|
|
|
public class MainActivity extends AppCompatActivity { |
|
|
|
private static final String TAG = "Lord"; |
|
|
|
EditText searchEditText; |
|
TextView titleTextview, authorTetview; |
|
|
|
@Override |
|
protected void onCreate (Bundle savedInstanceState) { |
|
super.onCreate(savedInstanceState); |
|
setContentView(R.layout.activity_main); |
|
|
|
searchEditText = findViewById(R.id.searchEditText); |
|
titleTextview = findViewById(R.id.titleTextview); |
|
authorTetview = findViewById(R.id.authorTextview); |
|
|
|
} |
|
|
|
public void searchBook (View view) { |
|
// Get the search string from the input field. |
|
String SEARCH_QUERY = searchEditText.getText().toString(); |
|
|
|
//Hiding the keyboard after the button pressed. |
|
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); |
|
if (inputManager != null) { |
|
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); |
|
} |
|
|
|
//Getting the network info and connectivity state: |
|
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
|
NetworkInfo networkInfo = null; |
|
if (connectivityManager != null) { |
|
networkInfo = connectivityManager.getActiveNetworkInfo(); |
|
} |
|
if (networkInfo != null && networkInfo.isConnected() && SEARCH_QUERY.length() != 0) { |
|
//Updating the text iews so that the user know what is going on. |
|
titleTextview.setText(R.string.loading); |
|
authorTetview.setText(R.string.please_wait); |
|
|
|
//Ask for the data over internet |
|
new FetchBook(titleTextview, authorTetview).execute(SEARCH_QUERY); |
|
} else { |
|
if (SEARCH_QUERY.length() == 0) { |
|
authorTetview.setText(""); |
|
titleTextview.setText(R.string.no_search_term); |
|
} else { |
|
authorTetview.setText(""); |
|
titleTextview.setText(R.string.no_connection); |
|
} |
|
} |
|
} |
|
|
|
public void searchVolley (View view) { |
|
|
|
titleTextview.setText("Volley In Progress"); |
|
// Base URL for Books API. |
|
String BOOK_BASE_URL = "https://www.googleapis.com/books/v1/volumes?"; |
|
// Parameter for the search string. |
|
String QUERY_PARAM = "q"; |
|
// Parameter that limits search results. |
|
String MAX_RESULTS = "maxResults"; |
|
// Parameter to filter by print type. |
|
String PRINT_TYPE = "printType"; |
|
|
|
// Get the search string from the input field. |
|
String SEARCH_QUERY = searchEditText.getText().toString(); |
|
|
|
Uri buildUri = Uri.parse(BOOK_BASE_URL).buildUpon() |
|
.appendQueryParameter(QUERY_PARAM, SEARCH_QUERY) |
|
.appendQueryParameter(MAX_RESULTS, "10") |
|
.appendQueryParameter(PRINT_TYPE, "books") |
|
.build(); |
|
|
|
// Instantiate the RequestQueue. |
|
RequestQueue requestQueue = Volley.newRequestQueue(this); |
|
String url = buildUri.toString(); |
|
|
|
// Request a string response from the provided URL. |
|
StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, url, new Response.Listener<String>() { |
|
@Override |
|
public void onResponse (String response) { |
|
//Parse the result |
|
parseResult(response); |
|
} |
|
}, new Response.ErrorListener() { |
|
@Override |
|
public void onErrorResponse (VolleyError error) { |
|
//Error if there volley error |
|
titleTextview.setText(error.toString()); |
|
} |
|
}); |
|
|
|
requestQueue.add(stringRequest); |
|
} |
|
|
|
private void parseResult (String response) { |
|
try { |
|
JSONObject jsonObject = new JSONObject(response); |
|
JSONArray jsonArray = jsonObject.getJSONArray("items"); |
|
int i = 0; |
|
String TITLE = null; |
|
String AUTHOR = null; |
|
|
|
while (i < jsonArray.length() && |
|
(AUTHOR == null && TITLE == null)) { |
|
// Get the current item information. |
|
JSONObject book = jsonArray.getJSONObject(i); |
|
JSONObject volumeInfo = book.getJSONObject("volumeInfo"); |
|
|
|
// Try to get the author and title from the current item, |
|
// catch if either field is empty and move on. |
|
try { |
|
TITLE = volumeInfo.getString("title"); |
|
AUTHOR = volumeInfo.getString("authors"); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
// Move to the next item. |
|
i++; |
|
} |
|
if (TITLE != null && AUTHOR != null) { |
|
titleTextview.setText(TITLE); |
|
authorTetview.setText(AUTHOR); |
|
} else { |
|
titleTextview.setText(R.string.no_result); |
|
authorTetview.setText(""); |
|
} |
|
} catch (JSONException e) { |
|
e.printStackTrace(); |
|
Log.e(TAG, "onPostExecute: ", e); |
|
// If onPostExecute does not receive a proper JSON string, |
|
// update the UI to show failed results. |
|
titleTextview.setText(R.string.no_result); |
|
authorTetview.setText(""); |
|
} |
|
} |
|
} |