-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from RENEEGAILP/A6
A6
- Loading branch information
Showing
9 changed files
with
201 additions
and
12 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...into/app/src/main/java/edu/neu/madcourse/numad21su_gailreneepinto/WebServiceActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package edu.neu.madcourse.numad21su_gailreneepinto; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.AsyncTask; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.ImageView; | ||
import android.widget.ProgressBar; | ||
|
||
import com.google.android.material.snackbar.Snackbar; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.ProtocolException; | ||
import java.net.URL; | ||
|
||
public class WebServiceActivity extends AppCompatActivity { | ||
|
||
ImageView HTTPResponseImageView; | ||
Button loadDataButton; | ||
EditText HTTPStatusCodeEditText; | ||
ProgressBar progressBar; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate( savedInstanceState ); | ||
setContentView( R.layout.activity_web_service ); | ||
HTTPResponseImageView = findViewById( R.id.http_image ); | ||
loadDataButton = findViewById( R.id.HTTP_status_load_image_button ); | ||
HTTPStatusCodeEditText = findViewById( R.id.HTTP_status_code_input_editText ); | ||
loadDataButton.setOnClickListener( this::onLoadDataButtonClick ); | ||
progressBar = findViewById( R.id.progressBar2 ); | ||
} | ||
|
||
public void onLoadDataButtonClick(View view) | ||
{ | ||
WebServiceTask webServiceTask = new WebServiceTask(); | ||
webServiceTask.execute( ); | ||
} | ||
|
||
private class WebServiceTask extends AsyncTask<Void, Void, Bitmap> { | ||
@Override | ||
protected void onPreExecute() { | ||
super.onPreExecute(); | ||
progressBar.setVisibility(View.VISIBLE); | ||
HTTPResponseImageView.setVisibility( View.INVISIBLE ); | ||
} | ||
|
||
@Override | ||
protected Bitmap doInBackground(Void... voids) { | ||
String url = "https://http.cat/"; | ||
String lHTTPStatusCode = HTTPStatusCodeEditText.getText().toString(); | ||
|
||
Bitmap bitmap = null; | ||
try { | ||
URL httpStatusURL =new URL(url+ lHTTPStatusCode +".jpg"); | ||
HttpURLConnection conn = (HttpURLConnection) httpStatusURL.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.setConnectTimeout(10000); | ||
conn.setReadTimeout(10000); | ||
conn.setDoInput(true); | ||
conn.connect(); | ||
|
||
InputStream inputStream = conn.getInputStream(); | ||
bitmap = BitmapFactory.decodeStream(inputStream); | ||
} catch (ProtocolException e) { | ||
e.printStackTrace(); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return bitmap; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Bitmap result) { | ||
super.onPostExecute( result ); | ||
progressBar.setVisibility( View.INVISIBLE ); | ||
HTTPResponseImageView.setImageBitmap( result ); | ||
if( result == null ) | ||
{ | ||
Snackbar.make( findViewById(R.id.layout), "Could not retrieve image. Please try again later", Snackbar.LENGTH_LONG ).show(); | ||
} | ||
HTTPResponseImageView.setVisibility( View.VISIBLE ); | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
NUMAD21Su-GailReneePinto/app/src/main/res/layout/activity_web_service.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<ScrollView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
tools:context=".WebServiceActivity" > | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:id="@+id/layout" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<TextView | ||
android:id="@+id/HTTP_status_code_textView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/HTTP_Status_code_message" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintVertical_bias="0.19" /> | ||
|
||
<ImageView | ||
android:id="@+id/http_image" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_centerHorizontal="true" | ||
android:layout_centerVertical="true" | ||
android:contentDescription="@string/HTTP_image_content_descriptor" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/HTTP_status_load_image_button" | ||
app:layout_constraintVertical_bias="0.62"></ImageView> | ||
|
||
<EditText | ||
android:id="@+id/HTTP_status_code_input_editText" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="16dp" | ||
android:ems="10" | ||
android:inputType="number" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.497" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/HTTP_status_code_textView" | ||
android:importantForAutofill="no" /> | ||
|
||
<Button | ||
android:id="@+id/HTTP_status_load_image_button" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/load_image_button_text" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/HTTP_status_code_input_editText" /> | ||
|
||
<ProgressBar | ||
android:id="@+id/progressBar2" | ||
style="?android:attr/progressBarStyle" | ||
android:layout_width="105dp" | ||
android:layout_height="106dp" | ||
android:visibility="gone" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/HTTP_status_load_image_button" | ||
app:layout_constraintVertical_bias="0.169" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
</ScrollView> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.