i am trying to get the app to connect to a localhost for login and registration purposes, but so far haven't had any luck. the app runs OK on the emulator but the connection kept refusing also on my phone the stack trace says connection timeout whereas on the emulator it doesnt print.
all the other exception are something that i am not familiar with as i am quiet new to android development
Register activity
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Register extends BaseActivity {
protected EditText username;
private EditText password;
private EditText email;
protected String enteredUsername;
private final String serverUrl = "connection to server";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
username = (EditText)findViewById(R.id.regName);
password = (EditText)findViewById(R.id.regpassword);
email = (EditText)findViewById(R.id.regEmail);
Button regsubmit = (Button)findViewById(R.id.buttonregister);
regsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enteredUsername = username.getText().toString();
String enteredPassword = password.getText().toString();
String enteredEmail = email.getText().toString();
if(enteredUsername.equals("") || enteredPassword.equals("") || enteredEmail.equals("")){
Toast.makeText(Register.this, "USERNAME OR PASSWORD REQUIRED", Toast.LENGTH_LONG).show();
return;
}
if(enteredUsername.length() <= 1 || enteredPassword.length() <= 1){
Toast.makeText(Register.this, "USERNAME OR PASSWORD MUST BE MORE THEN ONE CHARACTER", Toast.LENGTH_LONG).show();
return;
}
// request authentication with remote server4
AsyncDataClass asyncRequestObject = new AsyncDataClass();
asyncRequestObject.execute(serverUrl, enteredUsername, enteredPassword, enteredEmail);
}
});
}
private class AsyncDataClass extends AsyncTask<String , Void , String> {
@Override
protected String doInBackground(String... params) {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String result = "";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", params[1]));
nameValuePairs.add(new BasicNameValuePair("password", params[2]));
nameValuePairs.add(new BasicNameValuePair("email", params[3]));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
result = inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("Returned Json object " + result.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("result Value: " + result);
if(result.equals("") || result == null){
Toast.makeText(Register.this, "CONNECTION TO SERVER FAILED", Toast.LENGTH_LONG).show();
return;
}
int jsonResult = ParsedJsonObject(result);
if(jsonResult == 0){
Toast.makeText(Register.this, " INVALID EMAIL OR PASSWORD", Toast.LENGTH_LONG).show();
return;
}
if(jsonResult == 1){
Intent intent = new Intent(Register.this, Login.class);
intent.putExtra("USERNAME", enteredUsername);
intent.putExtra("MESSAGE", "YOUR REGISTRATION HAS BEEN SUCCESSFULL");
startActivity(intent);
}
}
}
private StringBuilder inputStreamToString(InputStream content) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(content));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return answer;
}
private int ParsedJsonObject(String result) {
JSONObject resultObject = null;
int returnedResult = 0;
try {
resultObject = new JSONObject(result);
returnedResult = resultObject.getInt("SUCCESS");
} catch (JSONException e) {
e.printStackTrace();
}
return returnedResult;
}
}