Skip to content

Instantly share code, notes, and snippets.

@byeblogs
Last active May 25, 2021 11:38
Show Gist options
  • Save byeblogs/b8186726a923c93755b747e7bb77495b to your computer and use it in GitHub Desktop.
Save byeblogs/b8186726a923c93755b747e7bb77495b to your computer and use it in GitHub Desktop.
Example Async Task Post
public static class HttpPostUploadImageResearch extends AsyncTask<String, String, Reptyr.AsyncTaskResult<JSONObject>> {
private static final String REQUEST_METHOD = "POST";
private static final int READ_TIMEOUT = 15000;
private static final int CONNECTION_TIMEOUT = 15000;
Activity activity;
public HttpPostUploadImageResearch(Activity activity)
{
super();
this.activity=activity;
}
@Override
protected Reptyr.AsyncTaskResult<JSONObject> doInBackground(String... params) {
try {
String url = params[0];
String data_src = params[1];
JSONObject result_object = null;
try {
//Create a URL object holding our url
URL myUrl = new URL(url);
JSONObject postDataParams = new JSONObject();
postDataParams.put("data_images", data_src);
Log.d(TAG, "Params HttpPostUploadImage" + postDataParams.toString());
//Create a connection
HttpURLConnection connection =(HttpURLConnection) myUrl.openConnection();
//Set methods and timeouts
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(Reptyr.getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = connection.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
result_object = new JSONObject(sb.toString());
}
else {
result_object = null;
}
}
catch(IOException | JSONException e){
e.printStackTrace();
}
// put your JSONObject here after fetch from the server
return new Reptyr.AsyncTaskResult<JSONObject>(result_object);
} catch ( Exception anyError) {
return new Reptyr.AsyncTaskResult<JSONObject>(anyError);
}
}
protected void onPostExecute(Reptyr.AsyncTaskResult<JSONObject> result) {
if ( result.getError() != null ) {
// error handling here
Log.d(TAG, "errorIn" + this.getClass().getSimpleName());
} else if ( isCancelled()) {
// cancel handling here
Log.d(TAG, "cancelledIn" + this.getClass().getSimpleName());
} else if(result.getResult()!=null){
JSONObject realResult = result.getResult();
Log.d(TAG, "Result Of Upload : " + realResult.toString());
image_uploaded += 1;
if(image_uploaded == total_image_upload){
this.activity.finish();
this.activity.overridePendingTransition(R.anim.stay, R.anim.slide_out_down);
}
} else {
// null handling here
}
};
};
public static class HttpPostUploadImage extends AsyncTask<String, String, Reptyr.AsyncTaskResult<JSONObject>> {
private static final String REQUEST_METHOD = "POST";
private static final int READ_TIMEOUT = 15000;
private static final int CONNECTION_TIMEOUT = 15000;
Activity activity;
public HttpPostUploadImage(Activity activity)
{
super();
this.activity=activity;
}
@Override
protected Reptyr.AsyncTaskResult<JSONObject> doInBackground(String... params) {
try {
String url = params[0];
String data_src = params[1];
String file_name = params[2];
JSONObject result_object = null;
try {
//Create a URL object holding our url
URL myUrl = new URL(url);
JSONObject postDataParams = new JSONObject();
postDataParams.put("data_src", data_src);
postDataParams.put("file_name", file_name);
Log.d(TAG, "Params HttpPostUploadImage" + postDataParams.toString());
//Create a connection
HttpURLConnection connection =(HttpURLConnection) myUrl.openConnection();
//Set methods and timeouts
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(Reptyr.getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = connection.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
result_object = new JSONObject(sb.toString());
}
else {
result_object = null;
}
}
catch(IOException | JSONException e){
e.printStackTrace();
}
// put your JSONObject here after fetch from the server
return new Reptyr.AsyncTaskResult<JSONObject>(result_object);
} catch ( Exception anyError) {
return new Reptyr.AsyncTaskResult<JSONObject>(anyError);
}
}
protected void onPostExecute(Reptyr.AsyncTaskResult<JSONObject> result) {
if ( result.getError() != null ) {
// error handling here
Log.d(TAG, "errorIn" + this.getClass().getSimpleName());
} else if ( isCancelled()) {
// cancel handling here
Log.d(TAG, "cancelledIn" + this.getClass().getSimpleName());
} else if(result.getResult()!=null){
JSONObject realResult = result.getResult();
Log.d(TAG, "Result Of Upload : " + realResult.toString());
image_uploaded += 1;
if(image_uploaded == total_image_upload){
this.activity.finish();
this.activity.overridePendingTransition(R.anim.stay, R.anim.slide_out_down);
}
} else {
// null handling here
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment