like this code
Code: Select all
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
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.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
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.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity implements OnClickListener {
private EditText user, pass;
private Button mSubmit, reg;
private CheckBox chk;
private ProgressDialog pDialog;
private String sessionUser;
Session session;
public static String validUsername, validPassword;
JSONParser jsonParser = new JSONParser();
//private static final String LOGIN_URL = "[url=http://10.0.2.2/ordering_api/login.php]http://10.0.2.2/ordering_api/login.php[/url]";
private static final String LOGIN_URL = "[url=http://122.52.217.71:8080/phpWidgetSource/loggedIN.php]http://122.52.217.71:8080/phpWidgetSo...[/url]";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
if (android.os.Build.VERSION.SDK_INT 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
chk = (CheckBox) findViewById(R.id.logCheck);
user = (EditText) findViewById(R.id.log_username);
pass = (EditText) findViewById(R.id.log_password);
mSubmit = (Button)findViewById(R.id.btnLogin);
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnLogin:
new AttemptLogin().execute();
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// Check for success tag
int success;
//String success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
List params = new ArrayList();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
String js = json.toString();
Log.d("Login attempt", json.toString());
Log.d("JSON:", js.replace("\\", ""));
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Session.setSessionUser(sessionUser);
Intent i = new Intent(Login.this, MainTabActivity.class);
validUsername = username;
validPassword = password;
saveToStorage();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
class FetchData extends AsyncTask {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
//String success;
String username = user.getText().toString();
String password = pass.getText().toString();
List params = new ArrayList();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
String js = json.toString();
Log.d("Login attempt", json.toString());
//Session.setSessionUser(sessionUser);
Intent i = new Intent(Login.this, MainTabActivity.class);
saveToStorage();
startActivity(i);
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute() {
runOnUiThread(new Runnable() {
public void run() {
saveToStorage();
finish();
passIntent();
}
});
}
}
private void passIntent() {
Intent intent = new Intent(this, MainTabActivity.class);
startActivity(intent);
}
private void saveToStorage() {
String content = loadFromNet("[url=http://122.52.217.71:8080/phpWidgetSource/logged.php?u]http://122.52.217.71:8080/phpWidgetSo...[/url]=" + validUsername + "&p=" + validPassword);
updateJSONData(content, "loggedin.json");
String notLogged = loadFromNet("[url=http://122.52.217.71:8080/phpWidgetSource/NotLoggedIN.php?u]http://122.52.217.71:8080/phpWidgetSo...[/url]=" + validUsername + "&p=" + validPassword);
updateJSONData(notLogged, "notloggedin.json");
String stardyIn = loadFromNet("[url=http://122.52.217.71:8080/phpWidgetSource/TardyIN.php?u]http://122.52.217.71:8080/phpWidgetSo...[/url]=" + validUsername + "&p=" + validPassword);
updateJSONData(stardyIn, "late.json");
}
private String loadFromNet(String url){
String result = null;
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
InputStream entityStream = entity.getContent();
StringBuilder entityStringBuilder = new StringBuilder();
byte [] buffer = new byte[1024];
int bytesReadCount;
while ((bytesReadCount = entityStream.read(buffer)) 0) {
entityStringBuilder.append(new String(buffer, 0, bytesReadCount));
}
result = entityStringBuilder.toString();
}catch(Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
return result;
}
public void updateJSONData(String stringToWrite, String filename) {
try {
File createFile = new File("mnt/sdcard/EDSCACHE/asset/" + filename);
FileWriter writeOutput = new FileWriter(createFile);
writeOutput.write(stringToWrite);
writeOutput.flush();
writeOutput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}