Java tutorial
/* * Copyright (C) 2015 Victor Apoyan. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package am.roadpolice.roadpolice; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.text.TextUtils; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import android.widget.Toast; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class DecisionDialogFragment extends DialogFragment { private WebView mWebView = null; private ProgressBar mProgressBar = null; private class DecisionDownloader extends AsyncTask<Void, Void, String> { private final String mPin; private DecisionDownloader(final String pin) { mPin = pin; } @Override protected String doInBackground(Void... params) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("https://offense.roadpolice.am/act.php"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("pin", mPin)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); java.util.Scanner s = new java.util.Scanner(response.getEntity().getContent()).useDelimiter("\\A"); return s.hasNext() ? s.next() : null; } catch (ClientProtocolException e) { } catch (IOException e) { } return null; } @Override protected void onPostExecute(String data) { // If download action was successfully completed, hide progress // bar and show content of decision in the web view in the dialog. if (mProgressBar != null) mProgressBar.setVisibility(View.GONE); if (mWebView != null && !TextUtils.isEmpty(data)) { mWebView.loadDataWithBaseURL(null, data, "text/html", "utf-8", null); mWebView.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) { view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } else { return false; } } }); } // Close Dialog in the case if data downloade from the server // is null or empty. else { final Activity activity = getActivity(); if (activity != null && isAdded()) { Toast.makeText(activity, getString(R.string.txtServerIssues), Toast.LENGTH_LONG).show(); } dismissAllowingStateLoss(); } } @Override protected void onCancelled() { final Activity activity = getActivity(); if (activity != null && isAdded()) { Toast.makeText(activity, getString(R.string.txtActionCanceled), Toast.LENGTH_LONG).show(); } } } private final static String TAG = DecisionDialogFragment.class.getSimpleName(); @Override public Dialog onCreateDialog(Bundle savedInstanceState) { View decisionView = getActivity().getLayoutInflater().inflate(R.layout.dialog_watcher, null); // Get Web View and Progress Bar. mWebView = (WebView) decisionView.findViewById(R.id.webView); mProgressBar = (ProgressBar) decisionView.findViewById(R.id.progressBar); Bundle bundle = getArguments(); final DecisionDownloader d = new DecisionDownloader(bundle.getString(ActionDialogFragment.EXTRA_PIN, "")); d.execute(); return new AlertDialog.Builder(getActivity()) .setNeutralButton(R.string.txtClose, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { d.cancel(true); } }).setView(decisionView).create(); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Override public void onDestroy() { Dialog dialog = getDialog(); if (dialog != null && dialog.isShowing()) dialog.dismiss(); super.onDestroy(); } }