-2

I have provided onCreate() and New password method where on debugging, it is crashing on getResponsecode(). It is crashing and not getting response. It is not going to get response from server.

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_password_activity);
        list(getIntent().getExtras().getString("JSON_Object"));
        CurrentPwdCheck();
        NewpwdCheck();

        btnSubmit= (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                try {
                    NewPwd();
                } catch (IOException e) {
                    e.printStackTrace();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void NewPwd() throws IOException, JSONException {

        String myPwd=new_pwd.getText().toString();

        String Surl="http://inmeets.com/ChangePwd.php?uid="+uid+"&NewPwd="+myPwd;

        URL url = null;
        HttpURLConnection conn = null;


            url = new URL(Surl);
        conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(1000);
       if( conn.getResponseCode()==203) {
           Toast.makeText(getBaseContext(), "your password changed", Toast.LENGTH_SHORT).show();
       }
    }
Kuldeep Kulkarni
  • 796
  • 5
  • 20

2 Answers2

0

You can not perform network actions from main thread because it will slow down the UI. Create a new AsyncTask to interact with your server.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
0

You can also use thread instead of AsyncTask. But AsyncTask would be preferably good because it is intelligent than thread in thread you have to manage all thing like run network call on background and update the UI in main thread. If you really want some different than AsyncTask bellow is the thread concept

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.change_password_activity);
            list(getIntent().getExtras().getString("JSON_Object"));
            CurrentPwdCheck();
            NewpwdCheck();

            btnSubmit= (Button) findViewById(R.id.btnSubmit);
            btnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    try {
                       NewPasword pwd = new NewPasword();
                       pwd.start(); 
                    } catch (IOException e) {
                        e.printStackTrace();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    class NewPasword extends Thread{
            @Override
            public void run() {
            String myPwd=new_pwd.getText().toString();

            String Surl="http://inmeets.com/ChangePwd.php?uid="+uid+"&NewPwd="+myPwd;

            URL url = null;
            HttpURLConnection conn = null;


                url = new URL(Surl);
            conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(1000);
           if( conn.getResponseCode()==203) {
     getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                                   Toast.makeText(getBaseContext(), "your password 
                     changed", Toast.LENGTH_SHORT).show();
                    }
                });

           }
            }
        }
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
Ashif Ahamad
  • 150
  • 1
  • 7