Skip to content

Instantly share code, notes, and snippets.

@arifullahjan
Created September 29, 2017 15:59
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 arifullahjan/a7d84114c29cc3566d80355c90ce557e to your computer and use it in GitHub Desktop.
Save arifullahjan/a7d84114c29cc3566d80355c90ce557e to your computer and use it in GitHub Desktop.
My way of doing same HTTP requests multiple times
public class SignIn extends AsyncTask<String, Void, String> {
private static final String TAG = "SignIn";
String email;
String password;
//outputs
User user;
boolean successStatus=false;
String message = "Failed";
public interface OnPostExecuteListener{
void onTaskDOne(boolean status, String message , User user);
}
SignIn.OnPostExecuteListener onPostExecuteListener;
public SignIn(String email,String password, SignIn.OnPostExecuteListener onPostExecuteListener) {
this.onPostExecuteListener = onPostExecuteListener;
this.email=email;
this.password=password;
}
public void getDetails() {
String result=null, line=null;
InputStream is = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("password", password));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://arifullahjan.com/api/signIn.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.d("Fail 1", e.toString());
}
try {
BufferedReader reader = new BufferedReader
(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.d(TAG,result);
} catch (Exception e) {
Log.d("Fail 2", e.toString());
}
try {
final JSONObject responseObject = new JSONObject(result);
message = responseObject.getString("message");
if (Integer.parseInt(responseObject.getString("success_status")) == 1){
successStatus=true;
JSONObject userObject = responseObject.getJSONObject("user");
Gson gson = new Gson();
user = gson.fromJson(userObject.toString(),User.class);
}
} catch (Exception e) {
Log.d("Fail 3", e.toString());
}
}
@Override
protected String doInBackground(String... params) {
getDetails();
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(onPostExecuteListener==null)return;
onPostExecuteListener.onTaskDOne(successStatus,message,user);
}
}
USAGE
public class Test{
//assume we have email and password
void SignIn(){
new SignIn(email, password, new SignIn.OnPostExecuteListener() {
@Override
public void onTaskDOne(boolean status,String password, final User user) {
if (status) {//if signin succeeded
runOnUiThread(new Runnable() {
@Override
public void run() {
loginSuccess(user);
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
error_text_view.setVisibility(View.VISIBLE);
error_text_view.startAnimation(AnimationUtils.loadAnimation(SignInActivity.this, android.R.anim.fade_in));
progressHide();
}
});
}
}
}).execute("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment