0
votes
  1. I'm doing a Small android Application. In that I'm storing The user entered Information to my local data base With Http Rest Call.
  2. There is nothing problem with the service It is working fine I tested In Browser and postman I'm able to Perform CRUD Operations through Postman Client.
  3. But When I try to POST OR GET From Android It is Throwing Error Like I/O error On Post method Connection Refused.
  4. I don't Know the Reason for this.
  5. Below Is My Code in Android.
  6. This code is for GET Request.

    public class Result extends AppCompatActivity { String ID; Login login=new Login(); final String url = "http://192.168.1.189:9001/login"; // List login = new ArrayList<>(); Button logout; TextView nameTv, ageTv, emailTv, usernameTv; String NAME, AGE, EMAIL, USERNAME;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       // Intent intent=getIntent();
       // ID=intent.getStringExtra("ID");
        // new HttpGetTask().execute();
    
        logout = (Button) findViewById(R.id.buttonLogout);
        nameTv = (TextView) findViewById(R.id.nameTextView);
        ageTv = (TextView) findViewById(R.id.ageTextView);
        emailTv = (TextView) findViewById(R.id.emailTextView);
        usernameTv = (TextView) findViewById(R.id.UsernameTextView);
        nameTv.setText(NAME);
        ageTv.setText(AGE);
        emailTv.setText(EMAIL);
        usernameTv.setText(USERNAME);
        new HttpRequestTask().execute();
    
    
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent logoutintent = new Intent(Result.this, MainActivity.class);
                startActivity(logoutintent);
            }
        });
    
    
    }
    
    private class HttpRequestTask extends AsyncTask<Void, Void, Login> {
    
    
        @Override
        protected Login doInBackground(Void... params) {
            try {
    
    
                //  final String url = "http://192.168.1.213:9001/consumer/local/64";
    
                RestTemplate restTemplate = new RestTemplate();
                Login lg = restTemplate.getForObject(url, Login.class);
               // NAME=lg.getName().toString();
                Log.d("", "doInBackground:++++++++++++++++++++++ "+NAME);
                return lg;
    
            } catch (Exception e) {
                Log.e("MainActivity", e.getMessage(), e);
    
            }
            return null;
        }
    

  1. This Below Code is for POST Request.

    public class Register extends AppCompatActivity { Button buttonRegister; public String ID; TextView editId,editName,editAge,editEmail,editUsername,editPassword; private String Name,Age,Email,Username,Password; Login login=new Login();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        editId=(TextView)findViewById(R.id.editId);
    
        editName=(TextView)findViewById(R.id.editName);
        editAge=(TextView)findViewById(R.id.editAge);
        editEmail=(TextView)findViewById(R.id.editEmail);
        editUsername=(TextView)findViewById(R.id.editUsername);
        editPassword=(TextView)findViewById(R.id.editPassword);
        buttonRegister = (Button)findViewById(R.id.buttonRegister);
    
        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
    
                Toast.makeText(Register.this, "Registration Successful", Toast.LENGTH_SHORT).show();
                Name = editName.getText().toString();
    
                Age = editAge.getText().toString();
                Email = editEmail.getText().toString();
                Username = editUsername.getText().toString();
                Password = editPassword.getText().toString();
    
                Log.d("???????????", "onClick:------------> " + Name);
                Log.d("???????????", "onClick:------------> " + ID);
                Log.d("???????????", "onClick:------------> " + Age);
                Log.d("???????????", "onClick:------------> " + Email);
                Log.d("???????????", "onClick:------------> " + Username);
                Log.d("???????????", "onClick:------------> " + Password);
                new HttpPostTask().execute();
                Intent regintent = new Intent(Register.this, Result.class);
                //regintent.putExtra("ID",1);
                startActivity(regintent);
    
                /*Intent regintent = new Intent(Register.this, Result.class);
                startActivity(regintent);*/
    
    
            }
        });
    
    
    
    
    }
    public class HttpPostTask extends AsyncTask<Void,Void,Login>{
    
        @Override
        protected Login doInBackground(Void... params) {
            final String url = "http://192.168.1.189:9001/login";
            RestTemplate restTemplate = new RestTemplate();
            Login login = new Login();
            login.setName(Name);
            login.setAge(Age);
            login.setEMail(Email);
            login.setUserName(Username);
            login.getId();
            login.setPassword(Password);
            Log.d("???????????", "onClick:!!!!!!!!!!!!!!> " + Name);
    
    
            login = restTemplate.postForObject(url, login, Login.class);
            ID=login.getId();
            Log.d("???????????", "onClick:!!!!!!!!!!!!!!> " + login.getId());
    
            return login;
        }
        @Override
        protected void onPostExecute(Login login){
            super.onPostExecute(login);
        }
    }
    

    }


  1. I have been searching for this problem since last two days.
  2. Any help appreciated Thanks In advance...........
1

1 Answers

1
votes
  1. Restarting my System Solve My Problem