Created
August 7, 2014 04:11
-
-
Save castrokingjames/db4394c83a8a1594db62 to your computer and use it in GitHub Desktop.
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.example.androidcallback.MainActivity" > | |
<TextView | |
android:id="@+id/textViewPokemonName" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignLeft="@+id/editTextPokemonId" | |
android:layout_alignParentTop="true" /> | |
<EditText | |
android:id="@+id/editTextPokemonId" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_below="@+id/textViewPokemonName" | |
android:layout_marginTop="10dp" | |
android:ems="10" | |
android:hint="Pokemon Id" /> | |
<Button | |
android:id="@+id/buttonSearchPokemonId" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignLeft="@+id/editTextPokemonId" | |
android:layout_alignRight="@+id/editTextPokemonId" | |
android:layout_below="@+id/editTextPokemonId" | |
android:layout_marginTop="10dp" | |
android:text="Search" /> | |
</RelativeLayout> |
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.example.androidcallback; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import android.os.AsyncTask; | |
public class HttpGetAsyncTask extends | |
AsyncTask<BasicNameValuePair, String, String> { | |
public final String url; | |
private OnHttpGetListener listener; | |
public HttpGetAsyncTask(String url) { | |
this.url = url; | |
} | |
@Override | |
protected String doInBackground(BasicNameValuePair... params) { | |
String returnValue = ""; | |
StringBuilder urlBuilder = new StringBuilder(); | |
urlBuilder.append(url); | |
for (BasicNameValuePair pair : params) { | |
urlBuilder.append(pair.getName()); | |
urlBuilder.append("="); | |
urlBuilder.append(pair.getValue()); | |
} | |
HttpClient client = new DefaultHttpClient(); | |
HttpGet get = new HttpGet(urlBuilder.toString()); | |
try { | |
StringBuilder responseBuilder = new StringBuilder(); | |
HttpResponse response = client.execute(get); | |
HttpEntity entity = response.getEntity(); | |
InputStream content = entity.getContent(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader( | |
content)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
responseBuilder.append(line); | |
} | |
returnValue = responseBuilder.toString(); | |
} catch (ClientProtocolException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return returnValue; | |
} | |
public void setOnHttpGetListener(OnHttpGetListener l) { | |
listener = l; | |
} | |
public void onPreExecute() { | |
if (listener != null) | |
listener.onHttpGetPreExecute(); | |
} | |
public void onPostExecute(String result) { | |
if (listener != null) | |
listener.onHttpGetSuccess(result); | |
} | |
public interface OnHttpGetListener { | |
public void onHttpGetSuccess(String result); | |
public void onHttpGetPreExecute(); | |
} | |
} |
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.example.androidcallback; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.app.Activity; | |
import android.app.ProgressDialog; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import com.example.androidcallback.HttpGetAsyncTask.OnHttpGetListener; | |
public class MainActivity extends Activity implements OnHttpGetListener, | |
OnClickListener { | |
TextView textViewPokemonName; | |
EditText editTextPokemonId; | |
Button buttonSearchPokemonId; | |
ProgressDialog progressDialog; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
textViewPokemonName = (TextView) findViewById(R.id.textViewPokemonName); | |
editTextPokemonId = (EditText) findViewById(R.id.editTextPokemonId); | |
buttonSearchPokemonId = (Button) findViewById(R.id.buttonSearchPokemonId); | |
buttonSearchPokemonId.setOnClickListener(this); | |
searchPokemonById(99); | |
} | |
private void searchPokemonById(int pokemonId) { | |
HttpGetAsyncTask task = new HttpGetAsyncTask( | |
"http://pokeapi.co/api/v1/pokemon/" + pokemonId); | |
task.setOnHttpGetListener(this); | |
task.execute(); | |
} | |
@Override | |
public void onHttpGetSuccess(String result) { | |
try { | |
JSONObject json = new JSONObject(result); | |
String pokemonName = json.getString("name"); | |
textViewPokemonName.setText(pokemonName); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
progressDialog.dismiss(); | |
} | |
@Override | |
public void onClick(View v) { | |
String id = editTextPokemonId.getText().toString(); | |
searchPokemonById(Integer.parseInt(id)); | |
} | |
@Override | |
public void onHttpGetPreExecute() { | |
progressDialog = ProgressDialog.show(this, "Pokemon Search", | |
"Searching Pokemon please wait", true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment