|
| 1 | +package com.lenovo.guessthecelebrity; |
| 2 | + |
| 3 | +import android.graphics.Bitmap; |
| 4 | +import android.graphics.BitmapFactory; |
| 5 | +import android.os.AsyncTask; |
| 6 | +import android.support.v7.app.AppCompatActivity; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.Button; |
| 10 | +import android.widget.ImageView; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.net.HttpURLConnection; |
| 17 | +import java.net.MalformedURLException; |
| 18 | +import java.net.URL; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Random; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.regex.Matcher; |
| 23 | +import java.util.regex.Pattern; |
| 24 | + |
| 25 | +public class MainActivity extends AppCompatActivity { |
| 26 | + |
| 27 | + ArrayList<String> celebURLs = new ArrayList<String>(); |
| 28 | + ArrayList<String> celebNames = new ArrayList<String>(); |
| 29 | + int chosenCeleb = 0; |
| 30 | + int locationOfCorrectAnswer = 0; |
| 31 | + String[] answers = new String[4]; |
| 32 | + |
| 33 | + ImageView imageView; |
| 34 | + Button button0; |
| 35 | + Button button1; |
| 36 | + Button button2; |
| 37 | + Button button3; |
| 38 | + |
| 39 | + public void celebChosen(View view) { |
| 40 | + |
| 41 | + if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))) { |
| 42 | + |
| 43 | + Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_LONG).show(); |
| 44 | + |
| 45 | + } else { |
| 46 | + |
| 47 | + Toast.makeText(getApplicationContext(), "Wrong! It was " + celebNames.get(chosenCeleb), Toast.LENGTH_LONG).show(); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + createNewQuestion(); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + public class ImageDownloader extends AsyncTask<String, Void, Bitmap> { |
| 56 | + |
| 57 | + |
| 58 | + @Override |
| 59 | + protected Bitmap doInBackground(String... urls) { |
| 60 | + |
| 61 | + try { |
| 62 | + |
| 63 | + URL url = new URL(urls[0]); |
| 64 | + |
| 65 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 66 | + |
| 67 | + connection.connect(); |
| 68 | + |
| 69 | + InputStream inputStream = connection.getInputStream(); |
| 70 | + |
| 71 | + Bitmap myBitmap = BitmapFactory.decodeStream(inputStream); |
| 72 | + |
| 73 | + return myBitmap; |
| 74 | + |
| 75 | + |
| 76 | + } catch (MalformedURLException e) { |
| 77 | + |
| 78 | + e.printStackTrace(); |
| 79 | + |
| 80 | + } catch (IOException e) { |
| 81 | + |
| 82 | + e.printStackTrace(); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + return null; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + public class DownloadTask extends AsyncTask<String, Void, String> { |
| 92 | + |
| 93 | + |
| 94 | + @Override |
| 95 | + protected String doInBackground(String... urls) { |
| 96 | + |
| 97 | + String result = ""; |
| 98 | + URL url; |
| 99 | + HttpURLConnection urlConnection = null; |
| 100 | + |
| 101 | + try { |
| 102 | + |
| 103 | + url = new URL(urls[0]); |
| 104 | + |
| 105 | + urlConnection = (HttpURLConnection)url.openConnection(); |
| 106 | + |
| 107 | + InputStream in = urlConnection.getInputStream(); |
| 108 | + |
| 109 | + InputStreamReader reader = new InputStreamReader(in); |
| 110 | + |
| 111 | + int data = reader.read(); |
| 112 | + |
| 113 | + while (data != -1) { |
| 114 | + |
| 115 | + char current = (char) data; |
| 116 | + |
| 117 | + result += current; |
| 118 | + |
| 119 | + data = reader.read(); |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | + |
| 124 | + } |
| 125 | + catch (Exception e) { |
| 126 | + |
| 127 | + e.printStackTrace(); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + return null; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + protected void onCreate(Bundle savedInstanceState) { |
| 137 | + super.onCreate(savedInstanceState); |
| 138 | + setContentView(R.layout.activity_main); |
| 139 | + |
| 140 | + imageView = (ImageView) findViewById(R.id.imageView); |
| 141 | + button0 = (Button) findViewById(R.id.button); |
| 142 | + button1 = (Button) findViewById(R.id.button2); |
| 143 | + button2 = (Button) findViewById(R.id.button3); |
| 144 | + button3 = (Button) findViewById(R.id.button4); |
| 145 | + |
| 146 | + DownloadTask task = new DownloadTask(); |
| 147 | + String result = null; |
| 148 | + |
| 149 | + try { |
| 150 | + |
| 151 | + result = task.execute("http://www.posh24.com/celebrities").get(); |
| 152 | + |
| 153 | + String[] splitResult = result.split("<div class=\"sidebarContainer\">"); |
| 154 | + |
| 155 | + Pattern p = Pattern.compile("<img src=\"(.*?)\""); |
| 156 | + Matcher m = p.matcher(splitResult[0]); |
| 157 | + |
| 158 | + while (m.find()) { |
| 159 | + |
| 160 | + celebURLs.add(m.group(1)); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + p = Pattern.compile("alt=\"(.*?)\""); |
| 165 | + m = p.matcher(splitResult[0]); |
| 166 | + |
| 167 | + while (m.find()) { |
| 168 | + |
| 169 | + celebNames.add(m.group(1)); |
| 170 | + |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + } catch (InterruptedException e) { |
| 175 | + |
| 176 | + e.printStackTrace(); |
| 177 | + |
| 178 | + } catch (ExecutionException e) { |
| 179 | + |
| 180 | + e.printStackTrace(); |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + createNewQuestion(); |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | + public void createNewQuestion() { |
| 189 | + |
| 190 | + Random random = new Random(); |
| 191 | + chosenCeleb = random.nextInt(celebURLs.size()); |
| 192 | + |
| 193 | + ImageDownloader imageTask = new ImageDownloader(); |
| 194 | + |
| 195 | + Bitmap celebImage; |
| 196 | + |
| 197 | + try { |
| 198 | + |
| 199 | + celebImage = imageTask.execute(celebURLs.get(chosenCeleb)).get(); |
| 200 | + |
| 201 | + imageView.setImageBitmap(celebImage); |
| 202 | + |
| 203 | + locationOfCorrectAnswer = random.nextInt(4); |
| 204 | + |
| 205 | + int incorrectAnswerLocation; |
| 206 | + |
| 207 | + for (int i=0; i<4; i++) { |
| 208 | + |
| 209 | + if (i == locationOfCorrectAnswer) { |
| 210 | + |
| 211 | + answers[i] = celebNames.get(chosenCeleb); |
| 212 | + |
| 213 | + } else { |
| 214 | + |
| 215 | + incorrectAnswerLocation = random.nextInt(celebURLs.size()); |
| 216 | + |
| 217 | + while (incorrectAnswerLocation == chosenCeleb) { |
| 218 | + |
| 219 | + incorrectAnswerLocation = random.nextInt(celebURLs.size()); |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | + answers[i] = celebNames.get(incorrectAnswerLocation); |
| 224 | + |
| 225 | + |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + } |
| 230 | + |
| 231 | + button0.setText(answers[0]); |
| 232 | + button1.setText(answers[1]); |
| 233 | + button2.setText(answers[2]); |
| 234 | + button3.setText(answers[3]); |
| 235 | + |
| 236 | + |
| 237 | + } catch (Exception e) { |
| 238 | + e.printStackTrace(); |
| 239 | + } |
| 240 | + |
| 241 | + |
| 242 | + |
| 243 | + |
| 244 | + } |
| 245 | +} |
0 commit comments