Created
December 31, 2015 10:46
-
-
Save samigehi/17af4711990fb8b5f001 to your computer and use it in GitHub Desktop.
Network helper class simply pass url and get json response
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.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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
e.g