Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.loginmysql;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- 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 org.json.JSONArray;
- import org.json.JSONObject;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class EditUser extends Activity {
- String password;
- String username;
- InputStream is=null;
- String result=null;
- String line=null;
- int code;
- int UserID;
- Activity context;
- HttpPost httppost;
- StringBuffer buffer;
- HttpResponse response;
- HttpClient httpclient;
- ProgressDialog pd;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.editnewuser);
- context=this;
- final EditText e_username=(EditText) findViewById(R.id.edtusername);
- final EditText e_password=(EditText) findViewById(R.id.edtnewpass);
- Button insert=(Button) findViewById(R.id.button1);
- Bundle extras = getIntent().getExtras();
- if(extras !=null)
- {
- e_username.setText(extras.getString("username"));
- e_password.setText(extras.getString("password"));
- UserID = extras.getInt("ID");
- }
- insert.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- username = e_username.getText().toString().trim();
- password = e_password.getText().toString().trim();
- Toast.makeText(getApplicationContext(), "username : "+username + "||" + "password : " + password,
- Toast.LENGTH_LONG).show();
- BackTask bt=new BackTask();
- bt.execute();
- }
- });
- }
- public void insert()
- {
- ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
- nameValuePairs.clear();
- nameValuePairs.add(new BasicNameValuePair("username",username));
- nameValuePairs.add(new BasicNameValuePair("password",password));
- nameValuePairs.add(new BasicNameValuePair("ID",String.valueOf(UserID)));
- try
- {
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://mobilekampus.com/ras/update_masteruser.php");
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- httpclient.execute(httppost);
- /* HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- is = entity.getContent();
- Log.e("pass 1", "connection success ");*/
- }
- catch(Exception e)
- {
- Log.e("Fail 1", e.toString());
- Toast.makeText(getApplicationContext(), "Invalid IP Address",
- Toast.LENGTH_LONG).show();
- }
- try
- {
- BufferedReader reader = new BufferedReader
- (new InputStreamReader(is,"iso-8859-1"),8);
- StringBuilder sb = new StringBuilder();
- while ((line = reader.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- is.close();
- result = sb.toString();
- Log.e("pass 2", "connection success ");
- }
- catch(Exception e)
- {
- Log.e("Fail 2", e.toString());
- }
- /*try
- {
- JSONObject json_data = new JSONObject(result);
- code=(json_data.getInt("code"));
- if(code==1)
- {
- Toast.makeText(getBaseContext(), "Inserted Successfully",
- Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(getBaseContext(), "Sorry, Try Again",
- Toast.LENGTH_LONG).show();
- }
- }
- catch(Exception e)
- {
- Log.e("Fail 3", e.toString());
- }*/
- }
- //background process to make a request to server and list product information
- private class BackTask extends AsyncTask<Void,Void,Void>{
- protected void onPreExecute(){
- super.onPreExecute();
- pd = new ProgressDialog(context);
- pd.setTitle("Updating ID : " + String.valueOf(UserID));
- pd.setMessage("Please wait.");
- pd.setCancelable(true);
- pd.setIndeterminate(true);
- pd.show();
- }
- protected Void doInBackground(Void...params){
- InputStream is=null;
- String result="";
- ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
- nameValuePairs.clear();
- nameValuePairs.add(new BasicNameValuePair("username",username));
- nameValuePairs.add(new BasicNameValuePair("password",password));
- nameValuePairs.add(new BasicNameValuePair("ID",String.valueOf(UserID)));
- try
- {
- httpclient = new DefaultHttpClient();
- httppost = new HttpPost("http://mobilekampus.com/ras/update_masteruser.php");
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- //httpclient.execute(httppost);
- response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- is = entity.getContent();
- Log.e("pass 1", "connection success ");
- }
- catch(Exception e)
- {
- if(pd!=null)
- pd.dismiss(); //close the dialog if error occurs
- Log.e("ERROR", e.getMessage());
- }
- //convert response to string
- try{
- BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null) {
- sb.append(line+"\n");
- }
- is.close();
- result=sb.toString();
- }catch(Exception e){
- Log.e("ERROR", "Error converting result "+e.toString());
- }
- //parse json data
- try{
- // Remove unexpected characters that might be added to beginning of the string
- result =result.substring(result.indexOf("["));
- JSONArray jArray =new JSONArray(result);
- for(int i=0;i<jArray.length();i++){
- JSONObject json_data =jArray.getJSONObject(i);
- code=(json_data.getInt("code"));
- }
- if(code==1)
- {
- Toast.makeText(getBaseContext(), "Updated Successfully",
- Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(getBaseContext(), "Sorry, Try Again",
- Toast.LENGTH_LONG).show();
- }
- }
- catch(Exception e){
- Log.e("ERROR", "Error pasting data "+e.toString());
- }
- return null;
- }
- protected void onPostExecute(Void result){
- if(pd!=null) pd.dismiss(); //close dialog
- context.startActivity(new Intent(context, ViewUser.class));
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public void onBackPressed() {
- // Do Here what ever you want do on back press;
- Toast.makeText(getApplicationContext(),"You are not allowed to go back!",Toast.LENGTH_LONG).show();
- }
- }
Add Comment
Please, Sign In to add comment