Skip to content

Instantly share code, notes, and snippets.

@mukundmadhav
Created July 12, 2017 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mukundmadhav/a60061bae0c7343c182fd95afeba601e to your computer and use it in GitHub Desktop.
Save mukundmadhav/a60061bae0c7343c182fd95afeba601e to your computer and use it in GitHub Desktop.
Guess the celebrity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="300dp"
app:srcCompat="@drawable/sample" />
<Button
android:id="@+id/b1"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Paris Hilton" />
<Button
android:id="@+id/b2"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Carly Rep" />
<Button
android:id="@+id/b3"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Taylor Swift" />
<Button
android:id="@+id/b4"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Karlie Kloss" />
</LinearLayout>
</RelativeLayout>
package me.mukundmadhav.guessthecelebrity;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
public String[] getNames(String sourceCode){
Pattern p = Pattern.compile("alt=\"(.*?)\"");
Matcher m = p.matcher(sourceCode);
String[] names = new String[50];
int i=0;
while (m.find()){
names[i]=m.group(1);
i++;
}
return names;
}
public class Background extends AsyncTask<String,Void,String[]>{
@Override
protected String[] doInBackground(String... imgurl) {
URL url;
HttpURLConnection httpURLConnection;
InputStream in;
InputStreamReader isr;
int data;
String sourceCode = "";
try{
url = new URL(imgurl[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
in = httpURLConnection.getInputStream();
isr = new InputStreamReader(in);
data = isr.read();
while(data!=-1){
sourceCode += (char) data;
data = isr.read();
}
return getNames(sourceCode);
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] code = new String[50];
Background obj = new Background();
try {
code = obj.execute("http://posh24.se/kandisar").get();
for (int i=0;i<50;++i)
Log.i("Names: ",code[i]);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment