Android中的Post Request Spring Boot Rest服务上的I / O错误

时间:2016-05-06 09:25:25

标签: java android spring rest

  1. 我正在做一个小型Android应用程序。因为我正在存储用户输入的信息到我的本地数据库使用Http Rest Call。
  2. 服务没有任何问题它工作正常我在浏览器和邮递员测试我能够通过Postman客户端执行CRUD操作。
  3. 但是当我尝试从Android发布或获取时它会像I / O错误那样抛出错误On Post方法Connection Refused。
  4. 我不知道原因。
  5. 以下是我的Android代码。
  6. 此代码用于GET请求。

    public class Result扩展AppCompatActivity {     字符串ID;     登录login = new登录();     final String url =“http://192.168.1.189:9001/login”;    // List login = new ArrayList<>();     按钮注销;     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. 以下代码适用于POST请求。

      public class Register扩展AppCompatActivity {     按钮按钮注册;     public String ID;     TextView editId,editName,editAge,editEmail,editUsername,editPassword;     私人字符串名称,年龄,电子邮件,用户名,密码;     登录login = new登录();

      @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. 自从过去两天以来,我一直在寻找这个问题。
      2. 任何帮助表示感谢提前...........

1 个答案:

答案 0 :(得分:1)

  1. 重新启动系统解决我的问题