- Forum posts: 1
Dec 18, 2014, 3:52:39 PM via Website
Dec 18, 2014 3:52:39 PM via Website
everytime i press the Submit button on my login, it closes, i dont know what is the prob here..
..i am using XAMPP(MySql and Apache are both started)..
..i am using localhost/phpmyadmin
..my database is named "newdatabase"
..my table for login is named "mytable"
..inside "mytable" is [ss_num] and [passs] (these are correct(ss_num = username | passs = password))
//start
package com.example.citizenschartersss;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
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.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Member_Log_In extends Activity implements OnClickListener{
private EditText user, pass;
private Button mSubmit, mRegister;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "h.t.t.p://10.0.2.2/adrx/logins.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member_log_in);
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
mSubmit = (Button)findViewById(R.id.bLogin);
mSubmit.setOnClickListener(this);
}
@override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==mSubmit){
new loglogin().execute();
}
}
class loglogin extends AsyncTask {
String username = user.getText().toString();
String password = pass.getText().toString();
@override
protected String doInBackground(String... args) {
int success;
try {
List params = new ArrayList();
params.add(new BasicNameValuePair("ss_num", username));
params.add(new BasicNameValuePair("passs", password));
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
// Log.d("request!", "starting");
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Toast.makeText(getApplicationContext(), "SUCCESS!", Toast.LENGTH_SHORT).show();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
Toast.makeText(getApplicationContext(), "FAILED!", Toast.LENGTH_SHORT).show();
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
}
// ----------------------------------------
this is my php code, logins.php
$response = array();
if (isset($_POST['ss_num']) && isset($_POST['passs'])) {
$ss_num = $_POST['ss_num'];
$passs = $_POST['passs'];
require_once DIR . '/connect.php';
$db = new DB_CONNECT();
$result = mysql_query("SELECT * FROM mytable WHERE ss_num='$ss_num' and passs='$passs'");
if ($result) {
$response["success"] = 1;
$response["message"] = "new data saved....";
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
echo json_encode($response);
}
}
//this is my JSONParser
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser(){
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
//----------
what is missing in the code? i cant seem to find the error.. please help, its for my thesis
P.S. : private static final String LOGIN_URL = "h.t.t.p://10.0.2.2/adrx/logins.php"; << dont mind this, i added dots to http cause of this >> "To prevent spam in our forums, new users may not post links to external sites."
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.