Last active
August 29, 2015 14:22
-
-
Save oshliaer/152e0d84b3529dd743e3 to your computer and use it in GitHub Desktop.
#android #AsyncTask #marvel
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.user.adroidtest; | |
import android.app.Activity; | |
import android.os.Bundle; | |
public class MainActivity extends Activity { | |
protected void onCreate(Bundle saveInstanceState) { | |
super.onCreate(saveInstanceState); | |
new RequestTask().execute("timestamp"); | |
} | |
} |
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.user.adroidtest; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
/** | |
* Created by http://stackoverflow.com/a/4846511/1393023 | |
*/ | |
public class md5 { | |
public static final String md5(final String s) { | |
final String MD5 = "MD5"; | |
try { | |
// Create MD5 Hash | |
MessageDigest digest = java.security.MessageDigest | |
.getInstance(MD5); | |
digest.update(s.getBytes()); | |
byte messageDigest[] = digest.digest(); | |
// Create Hex String | |
StringBuilder hexString = new StringBuilder(); | |
for (byte aMessageDigest : messageDigest) { | |
String h = Integer.toHexString(0xFF & aMessageDigest); | |
while (h.length() < 2) | |
h = "0" + h; | |
hexString.append(h); | |
} | |
return hexString.toString(); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
} |
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.user.adroidtest; | |
import android.net.Uri; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
/** | |
* Created by User on 28.05.2015. | |
*/ | |
class RequestTask extends AsyncTask<String, Void, String> { | |
private final String LOG_TAG = RequestTask.class.getSimpleName(); | |
@Override | |
protected String doInBackground(String... params) { | |
if (params.length == 0) { | |
return null; | |
} | |
HttpURLConnection urlConnection = null; | |
BufferedReader reader = null; | |
String jsonStr = null; | |
String privateKey = "{PRIVATE_KEY}"; | |
String apiKey = "{API_KEY}"; | |
try { | |
final String BASE_URL = "http://gateway.marvel.com/v1/public/characters?"; | |
final String TS_PARAM = "ts"; | |
final String API_KEY_PARAM = "apikey"; | |
final String HASH_PARAM = "hash"; | |
Uri builtUri = Uri.parse(BASE_URL).buildUpon() | |
.appendQueryParameter(TS_PARAM, params[0]) | |
.appendQueryParameter(API_KEY_PARAM, apiKey) | |
.appendQueryParameter(HASH_PARAM, md5.md5(params[0] + privateKey + apiKey)) | |
.build(); | |
URL url = new URL(builtUri.toString()); | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
urlConnection.setRequestMethod("GET"); | |
urlConnection.connect(); | |
// Read the input stream into a String | |
InputStream inputStream = urlConnection.getInputStream(); | |
StringBuffer buffer = new StringBuffer(); | |
if (inputStream == null) { | |
// Nothing to do. | |
return null; | |
} | |
reader = new BufferedReader(new InputStreamReader(inputStream)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
buffer.append(line + "\n"); | |
} | |
if (buffer.length() == 0) { | |
return null; | |
} | |
jsonStr = buffer.toString(); | |
} catch (IOException e) { | |
Log.e(LOG_TAG, "Error ", e); | |
return null; | |
} finally { | |
if (urlConnection != null) { | |
urlConnection.disconnect(); | |
} | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (final IOException e) { | |
Log.e(LOG_TAG, "Error closing stream", e); | |
} | |
} | |
} | |
Log.i(LOG_TAG, jsonStr); | |
return jsonStr; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
super.onPostExecute(result); | |
//Do anything with response.. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment