Last active
April 17, 2018 13:32
-
-
Save jmarkman/733b0a1cbeb41a1c5733c0c715e4606b to your computer and use it in GitHub Desktop.
My main activity that uses the DogAPI I made
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
package com.jmarkman.dog; | |
import android.net.Uri; | |
import android.os.AsyncTask; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.squareup.picasso.Picasso; | |
import org.w3c.dom.Text; | |
import java.io.IOException; | |
import java.net.URL; | |
public class Dog extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_dog); | |
} | |
public void clickDog(View view) | |
{ | |
new DogTask().execute(); | |
} | |
public class DogTask extends AsyncTask<Void, Void, Uri> | |
{ | |
private DogAPI dogAPI; | |
@Override | |
protected Uri doInBackground(Void... voids) | |
{ | |
String json = ""; | |
Uri imageURL = null; | |
dogAPI = new DogAPI(); | |
URL url = dogAPI.getDogURL(); | |
try | |
{ | |
json = dogAPI.getJSON(url); | |
imageURL = dogAPI.getImage(json); | |
} | |
catch (IOException ioe) | |
{ | |
ioe.printStackTrace(); | |
} | |
return imageURL; | |
} | |
protected void onPostExecute(Uri uri) | |
{ | |
ImageView dogImgView = findViewById(R.id.dog_image); | |
Picasso.with(dogImgView.getContext()) | |
.load(uri) | |
.into(dogImgView); | |
TextView urlText = findViewById(R.id.picture_url); | |
urlText.setText(uri.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment