Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package thieu.mao.xosoviet;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.NameValuePair;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnClickListener {
- private Button btHomNay, btKetQua, btKetQuaDai;
- private ProgressDialog dialog;
- private String[] mangKetQua;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setTitle("Xổ Số Việt");
- btHomNay = (Button) findViewById(R.id.buttonHomNay);
- btKetQua = (Button) findViewById(R.id.buttonKetQua);
- btKetQuaDai = (Button) findViewById(R.id.buttonKetQuaDai);
- btKetQua.setOnClickListener(this);
- btHomNay.setOnClickListener(this);
- btKetQuaDai.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.buttonKetQua:
- // dialog = ProgressDialog.show(this, "", "Loanding ...");
- if (isNetAvailable(getApplicationContext())) {
- String tinh = "xo-so-mien-bac";
- String linkMienBac = "http://tiasangmoi.com/GetWebData/layketquaxoso24h/linklay24h/layChung.php?link=http://ketquaxoso.24h.com.vn/"
- + tinh + "/";
- new CategoryTask().execute(linkMienBac);
- } else {
- Toast.makeText(this, "Please check 3G/Wifi", Toast.LENGTH_SHORT)
- .show();
- }
- break;
- case R.id.buttonHomNay:
- Intent intentHomNay = new Intent(MainActivity.this,
- HomNayActivity.class);
- startActivity(intentHomNay);
- break;
- case R.id.buttonKetQuaDai:
- Intent intentKetQuaDai = new Intent(MainActivity.this,
- KetQuaDaiActivity.class);
- startActivity(intentKetQuaDai);
- break;
- default:
- break;
- }
- }
- public Boolean isNetAvailable(Context con) {
- try {
- ConnectivityManager connectivityManager = (ConnectivityManager) con
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo wifiInfo = connectivityManager
- .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- NetworkInfo mobileInfo = connectivityManager
- .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
- if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
- return true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- class CategoryTask extends AsyncTask<String, Void, Void> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- dialog = new ProgressDialog(MainActivity.this);
- dialog.setMessage("Loading...");
- dialog.setCancelable(true);
- dialog.show();
- }
- @Override
- protected Void doInBackground(String... params2) {
- String[] arrResult = new String[5];
- try {
- String url_all_products = params2[0];
- boolean checkMienBac = url_all_products.contains("mien-bac");
- JSONParser jParser = new JSONParser();
- // Building Parametersd
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- JSONObject json = jParser.makeHttpRequest(url_all_products,
- "GET", params);
- // Check your log cat for JSON reponse
- Log.d("All Products: ", json.toString());
- // products JSONArray
- JSONArray products = null;
- products = json.getJSONArray("message");
- for (int i = 0; i < products.length(); i++) {
- JSONObject c = products.getJSONObject(i);
- if (checkMienBac) {
- String Time = c.getString("Time");
- String DB = c.getString("DB");
- String G1 = c.getString("G1");
- String G2 = c.getString("G2");
- String G3 = c.getString("G3");
- String G4 = c.getString("G4");
- String G5 = c.getString("G5");
- String G6 = c.getString("G6");
- String G7 = c.getString("G7");
- String strResult = Time + "," + DB + "," + G1 + ","
- + G2 + "," + G3 + "," + G4 + "," + G5 + ","
- + G6 + "," + G7;
- arrResult[i] = strResult;
- Log.d("Ket qua: ", strResult);
- } else {
- String Time = c.getString("Time");
- String DB = c.getString("DB");
- String G1 = c.getString("G1");
- String G2 = c.getString("G2");
- String G3 = c.getString("G3");
- String G4 = c.getString("G4");
- String G5 = c.getString("G5");
- String G6 = c.getString("G6");
- String G7 = c.getString("G7");
- String G8 = c.getString("G8");
- String strResult = Time + "," + DB + "," + G1 + ","
- + G2 + "," + G3 + "," + G4 + "," + G5 + ","
- + G6 + "," + G7 + "," + G8;
- arrResult[i] = strResult;
- }
- }
- mangKetQua = arrResult;
- } catch (JSONException e) {
- e.printStackTrace();
- Log.d("Ket qua: ", "Loi roi");
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- if (dialog != null) {
- dialog.dismiss();
- }
- Intent intent = new Intent(MainActivity.this, KetQuaActivity.class);
- intent.putExtra("mKQ", mangKetQua);
- startActivity(intent);
- super.onPostExecute(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment