Last active
March 15, 2018 01:59
-
-
Save jcortesg/3184680 to your computer and use it in GitHub Desktop.
json
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.example.login; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.app.Activity; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
public class ActRegistro extends Activity { | |
// Progress Dialog | |
public final static String Exta_mes = "com.example.login.hola"; | |
private ProgressDialog pDialog; | |
JSONParser jsonParser = new JSONParser(); | |
EditText inputPid; | |
EditText inputFirstName; | |
EditText inputLastName; | |
EditText inputEmail; | |
EditText inputPhone; | |
EditText inputCellPhone; | |
EditText inputCountry; | |
EditText inputState; | |
EditText inputCity; | |
EditText inputAddressHome; | |
EditText inputAddressJob; | |
EditText inputPassword; | |
Button btnRegister; | |
Button btnLinkToLogin; | |
public void volver(View view) { | |
Intent intLogin = new Intent (this, ActLogin.class); | |
startActivity(intLogin); | |
} | |
// url to create new product | |
private static String url_create_user = "http://androidws.awardspace.biz/create_user.php"; | |
// JSON Node names | |
private static final String TAG_SUCCESS = "success"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Intent intent = getIntent(); | |
// String mensaje = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); | |
setContentView(R.layout.act_registro); | |
// Edit Text | |
inputPid = (EditText) findViewById(R.id.edit_id); | |
inputFirstName = (EditText) findViewById(R.id.edit_fist_name); | |
inputLastName = (EditText) findViewById(R.id.edit_last_name); | |
inputEmail = (EditText) findViewById(R.id.edit_email); | |
inputPhone = (EditText) findViewById(R.id.edit_phone); | |
inputCellPhone = (EditText) findViewById(R.id.edit_cell_phone); | |
inputCountry = (EditText) findViewById(R.id.edit_country); | |
inputState = (EditText) findViewById(R.id.edit_state); | |
inputCity = (EditText) findViewById(R.id.edit_city); | |
inputAddressHome = (EditText) findViewById(R.id.edit_address_home); | |
inputAddressJob = (EditText) findViewById(R.id.edit_address_job); | |
inputPassword = (EditText) findViewById(R.id.edit_pwd); | |
// Importing all assets like buttons, text fields | |
btnRegister = (Button) findViewById(R.id.btn_registrar); | |
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreens); | |
// EditText editText = (EditText)findViewById(R.id.edit_name); | |
// editText.setText(mensaje); | |
// Register Button Click event | |
btnRegister.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
// creating new product in background thread | |
new CreateNewUser().execute(); | |
// String name = inputFullName.getText().toString(); | |
// String email = inputEmail.getText().toString(); | |
// String password = inputPassword.getText().toString(); | |
// UserFunctions userFunction = new UserFunctions(); | |
// JSONObject json = userFunction.registerUser(name, email, | |
// password); | |
// | |
// // check for login response | |
// try { | |
// if (json.getString(KEY_SUCCESS) != null) { | |
// registerErrorMsg.setText(""); | |
// String res = json.getString(KEY_SUCCESS); | |
// if (Integer.parseInt(res) == 1) { | |
// // user successfully registred | |
// // Store user details in SQLite Database | |
// DatabaseHandler db = new DatabaseHandler( | |
// getApplicationContext()); | |
// JSONObject json_user = json.getJSONObject("user"); | |
// | |
// // Clear all previous data in database | |
// userFunction.logoutUser(getApplicationContext()); | |
// db.addUser(json_user.getString(KEY_NAME), | |
// json_user.getString(KEY_EMAIL), | |
// json.getString(KEY_UID), | |
// json_user.getString(KEY_CREATED_AT)); | |
// // Launch Dashboard Screen | |
// Intent dashboard = new Intent( | |
// getApplicationContext(), | |
// DashboardActivity.class); | |
// // Close all views before launching Dashboard | |
// dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
// startActivity(dashboard); | |
// // Close Registration Screen | |
// finish(); | |
// } else { | |
// // Error in registration | |
// registerErrorMsg | |
// .setText("Error occured in registration"); | |
// } | |
// } | |
// } catch (JSONException e) { | |
// e.printStackTrace(); | |
// } | |
} | |
}); | |
// Link to Login Screen | |
// btnLinkToLogin.setOnClickListener(new View.OnClickListener() { | |
// public void onClick(View view) { | |
// Intent intLogin = new Intent (this, ActLogin.class); | |
// startActivity(intLogin); | |
// // Close Registration View | |
// //startActivity(intLogin); | |
// //finish(); | |
// } | |
// }); | |
} | |
/** | |
* Background Async Task to Create new user | |
* */ | |
class CreateNewUser extends AsyncTask<String, String, String> { | |
/** | |
* Before starting background thread Show Progress Dialog | |
* */ | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
pDialog = new ProgressDialog(ActRegistro.this); | |
pDialog.setMessage("Creating User.."); | |
pDialog.setIndeterminate(false); | |
pDialog.setCancelable(true); | |
pDialog.show(); | |
} | |
/** | |
* Creating user | |
* */ | |
protected String doInBackground(String... args) { | |
// Building Parameters | |
List<NameValuePair> params = new ArrayList<NameValuePair>(); | |
params.add(new BasicNameValuePair("pid", inputPid.getText().toString())); | |
params.add(new BasicNameValuePair("first_name", inputFirstName.getText().toString())); | |
params.add(new BasicNameValuePair("last_name", inputLastName.getText().toString())); | |
params.add(new BasicNameValuePair("email", inputEmail.getText().toString())); | |
params.add(new BasicNameValuePair("phone", inputPhone.getText().toString())); | |
params.add(new BasicNameValuePair("cell_phone", inputCellPhone.getText().toString())); | |
params.add(new BasicNameValuePair("country", inputCountry.getText().toString())); | |
params.add(new BasicNameValuePair("state", inputState.getText().toString())); | |
params.add(new BasicNameValuePair("city", inputCity.getText().toString())); | |
params.add(new BasicNameValuePair("address_home", inputAddressHome.getText().toString())); | |
params.add(new BasicNameValuePair("address_job", inputAddressJob.getText().toString())); | |
params.add(new BasicNameValuePair("password", inputPassword.getText().toString())); | |
// getting JSON Object | |
// Note that create product url accepts POST method | |
JSONObject json = jsonParser.makeHttpRequest(url_create_user, "POST", params); | |
// check log cat fro response | |
Log.d("Create Response", json.toString()); | |
// check for success tag | |
try { | |
int success = json.getInt(TAG_SUCCESS); | |
if (success == 1) { | |
// successfully created product | |
// Intent i = new Intent(getApplicationContext(), AllProductsActivity.class); | |
// startActivity(i); | |
// closing this screen | |
// finish(); | |
} else { | |
// failed to create user | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
/** | |
* After completing background task Dismiss the progress dialog | |
* **/ | |
protected void onPostExecute(String file_url) { | |
// dismiss the dialog once done | |
pDialog.dismiss(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment