JSON Helper Class
public class JSONHelper extends AsyncTask<Void, Void, String> {
Context context;
String myUrl;
ProgressDialog progressDialog;
OnAsyncLoader onAsyncLoader;
HashMap<String, String> hashMap;
JSONObject hashMapWithJson;
boolean isProgressVisible;
public JSONHelper(Context context, String url, HashMap<String, String> hashMap, OnAsyncLoader onAsynckLoader, boolean isProgressVisible) {
this.context = context;
myUrl = url;
this.onAsyncLoader = onAsynckLoader;
this.hashMap = hashMap;
this.isProgressVisible = isProgressVisible;
}
public JSONHelper(Context context, String url, HashMap<String, String> hashMap, OnAsyncLoader onAsynckLoader, boolean isProgressVisible, JSONObject jsonObj) {
this.context = context;
myUrl = url;
this.onAsyncLoader = onAsynckLoader;
this.hashMap = hashMap;
this.isProgressVisible = isProgressVisible;
this.hashMapWithJson = jsonObj;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (isProgressVisible) {
progressDialog = new ProgressDialog(context);
if(Constants.hashMap.containsKey("nop.progressdilog.title"))
progressDialog.setMessage(Constants.hashMap.get("nop.progressdilog.title"));
else
progressDialog.setMessage("Please wait a moment");
progressDialog.setCancelable(false);
progressDialog.show();
}
}
@Override
protected String doInBackground(Void... params) {
String result = "";
try {
URL url = new URL(myUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
if (context!=null) {
Log.d("JSON_HELPER", url.toString());
}
if (hashMap != null) {
httpURLConnection.setReadTimeout(20000);
httpURLConnection.setConnectTimeout(20000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
Log.d("LOGDATA", writer.toString());
writer.write(getPostDataString(hashMap));
writer.flush();
writer.close();
os.close();
}
if (hashMapWithJson != null) {
httpURLConnection.setReadTimeout(20000);
httpURLConnection.setConnectTimeout(20000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Accept", "application/json");
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes(hashMapWithJson.toString());
/*OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(hashMapWithJson.toString());*/
// writer.write(getPostDataString(hashMap));
wr.flush();
wr.close();
// os.close();
}
if (httpURLConnection.getResponseCode() == 200) {
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
}
httpURLConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (isProgressVisible) {
progressDialog.dismiss();
}
try {
onAsyncLoader.OnResult(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}