I have a php file setup with login and authentication via server. I am trying to do POST HTTP connection to that login.php file and verify login authentication. I might sound quite naive in asking question, please let me know if I have to clarify more.
package com.myproject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import org.apache.http.HttpConnection;
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.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.animation.ArgbEvaluator;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Telephony.Sms.Conversations;
import android.util.Log;
import android.util.MalformedJsonException;
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.Toast;
public class MainActivity extends Activity implements OnClickListener {
private String mTitle = "Write.My.Action";
private static final String LOGTAG = "tag";
public EditText username, password;
private Button login, register;
private ProgressDialog mDialog;
JSONParser jsonParser = new JSONParser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setTitle(mTitle);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.register);
login.setOnClickListener(this);
register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.login:
if((username.getText().toString().length() < 1) || (password.getText().toString().length() < 1)){
Toast.makeText(MainActivity.this, "Please enter username and password", Toast.LENGTH_LONG).show();
}else{
new AttemptLogin().execute();
break;
}
case R.id.register:
//Intent intent = new Intent(MainActivity.this, Register.class);
//startActivity(intent);
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class AttemptLogin extends AsyncTask<String, String, String>{
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog = new ProgressDialog(MainActivity.this);
mDialog.setMessage("Attempting to Login...");
mDialog.setIndeterminate(false);
mDialog.setCancelable(false);
mDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
try {
String input_username = username.getText().toString();
String input_password = password.getText().toString();
//Log.i(LOGTAG, "Username is "+ input_username);
String link = "http://psmovers.com.au/login.php";
String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(input_username, "UTF-8");
data+="&" +URLEncoder.encode("password", "UTF-8")+ "=" + URLEncoder.encode(input_password, "UTF-8");
//Log.i(LOGTAG, "Data is "+ data);
URL url = new URL(link);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
// Log.i(LOGTAG, "Wr value is " + wr);
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
//Read server response
while((line=reader.readLine()) != null){
sb.append(line + "\n");
break;
}
System.out.print("The result after while loop: " + sb.toString());
} catch (Exception e) {
Log.i(LOGTAG, "Exception:" + e.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
mDialog.setTitle("Login Success");
mDialog.dismiss();
Toast.makeText(MainActivity.this, "Result is :" + result, Toast.LENGTH_LONG).show();
}
}
}
PLEASE NOTE: I have INTERNET permission setup in Manifest file, It is also reading the URL, successfully getting text input from devise, also I am able to open the above URL link independently on my tablet..
System.out.print("The result after while loop: " + sb.toString());
The output in Logcat is: The result after the while loop : <h1>Login</h1>
even the toast message onPostexeecute method id
Result is : <h1>Login</h1>
break;
in while loop is not necessary.
while((line=reader.readLine()) != null){
sb.append(line + "\n");
// break;
}