Skip to content

Instantly share code, notes, and snippets.

@samigehi
Created December 31, 2015 10:46
Show Gist options
  • Save samigehi/17af4711990fb8b5f001 to your computer and use it in GitHub Desktop.
Save samigehi/17af4711990fb8b5f001 to your computer and use it in GitHub Desktop.
Network helper class simply pass url and get json response
package com.sumeet;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.intelligenes.alexxo.activities.NavigationActivity;
import android.os.AsyncTask;
// @auther sumeet.gehi
// 20-11-15
public class NetworkTask extends AsyncTask<Object, Integer, Object> {
onTaskComplete complete;
String url = "";
private boolean isPost;
public NetworkTask(String server) {
url = server;
}
public AsyncTask<Object, Integer, Object> run(Object... params) {
LOG.e(getClass().getCanonicalName(), "server == " + url);
return executeOnExecutor(THREAD_POOL_EXECUTOR, params);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object... params) {
JsonArray array = null;
try {
if (Utils.ping(NavigationActivity.URL, JSON.DEFAULT_PING)) {
URL Url = new URL(url);
URLConnection urlConnection = Url.openConnection();
if (isPost) {
HttpURLConnection con = (HttpURLConnection) Url.openConnection();
con.setRequestMethod("POST");
urlConnection = con;
}
urlConnection.setDoInput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line = reader.readLine();
try {
array = parse(line);
} catch (Exception e) {
e.printStackTrace();
return line;
}
return array;
}
} catch (Exception e1) {
e1.printStackTrace();
}
return array;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (result != null && complete != null)
complete.complete(result);
else {
if (complete != null)
complete.complete(null);
}
}
private JsonArray parse(Object object) throws JsonParseException, JsonSyntaxException {
JsonArray array = new JsonArray();
JsonElement element = new JsonParser().parse((String) object);
if (element instanceof JsonArray) {
array.add(((JsonArray) element).get(0));
} else if (element instanceof JsonObject) {
array.add(element.getAsJsonObject());
}
LOG.d("NetworkTask--JsonArray", array.toString());
return array;
}
public void SetOnCompleteListner(onTaskComplete listner) {
complete = listner;
}
public boolean isPost() {
return isPost;
}
public void setPost(boolean isPost) {
this.isPost = isPost;
}
interface onTaskComplete {
void complete(Object result);
}
}
@samigehi
Copy link
Author

samigehi commented Dec 31, 2015

e.g

       NetworkTask task = new NetworkTask(url-here);
       task.SetOnCompleteListner(new NetworkTask.onTaskComplete() {
         @Override
                public void complete(Object result) {
                    if (result != null) {
                        if (result instanceof JsonArray) {
                            JsonArray array = (JsonArray) result;
                            LOG.info(tag + "Approval array-" + array.toString());
                        } else if (result instanceof JsonObject) {
                            JsonObject object = (JsonObject) result;
                            LOG.info(tag + " approval object-" + object.toString());
                        } else if (result instanceof String) {
                            LOG.info(tag + "String-" + result);
                        }
                    }
                }
            });
            task.run("");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment