1

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;
    }
}
Emotional_Goose
  • 167
  • 1
  • 13

1 Answers1

1

StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.

StrictMode is most commonly used to catch accidental disk or network access on the application's main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications. By keeping your application's main thread responsive, you also prevent ANR dialogs from being shown to users.

Source.

Since you have this strict mode violation message:

StrictMode policy violation

it is safe to assume that you have made a mistake by accident which can be detected if you start your research about StrictMode in general and your occurrence in particular.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Thnx really appreciate it. – Emotional_Goose Dec 29 '15 at 00:54
  • so far i figured that if i run the application on my phone then i don't get the strict mode error but get the rest of the errors . also i already tried to tick the box but cant as i need to have 15 reputation. this is first time i am using stack overflow. – Emotional_Goose Dec 29 '15 at 02:27
  • No problems, welcome to stackoverflow! Since up until now this answer was helpful, but did not represent a solution, you should not accept it. We should discuss the problem, possibly with other participants until we know the 100% solution. As about points, if you answer a few simpler questions, you will get some reputation. For instance, this question was not bad, therefore I am upvoting it. – Lajos Arpad Dec 29 '15 at 02:32
  • just going through the tutorial which will give me some reputation. i do have sufficient knowledge in web development so hopefully can answer some related question. – Emotional_Goose Dec 29 '15 at 02:40
  • @wazzkhan, you are not calling a release of something somewhere. Can you point out the lines where you are having the problems? – Lajos Arpad Dec 29 '15 at 03:16
  • resultObject = new JSONObject(result); it points at this first then this int jsonResult = ParsedJsonObject(result); both of these code fragment are on register activity – Emotional_Goose Dec 29 '15 at 03:23
  • What is the value of result? It seems that you are having some problems with converting it to JSON Object. – Lajos Arpad Dec 29 '15 at 03:29
  • just added the whole stack trace, the value supposed to be username = wur1, email = wur1@student and password = wur123 – Emotional_Goose Dec 29 '15 at 03:33
  • This is key: org.json.JSONException: Value this of type java.lang.String cannot be converted to JSONObject (http://stackoverflow.com/questions/10267910/jsonexception-value-of-type-java-lang-string-cannot-be-converted-to-jsonobject). It might be the main cause of your problem. Can you check result, by debugging? I understand what it is supposed to hold, but what if its format is incorrect? – Lajos Arpad Dec 29 '15 at 03:42
  • yeh you were right the format was wrong, username attribute in php script was miss spelled. so its works not. there are some other exception, but they are minor. thank you for your help once again really helped me break down this issue – Emotional_Goose Dec 29 '15 at 04:14
  • I am glad I could contribute to the solution – Lajos Arpad Dec 29 '15 at 04:27