Post via Json getstring error

0

I have a code that should do a "Post" via json, but edittext is not converting to String, and there is no error in logcat .. Anyone have an idea of the error? It must be stupid, I apologize, but I'm new on android.

public class PostTeste extends AppCompatActivity {


    private Button btnfincad;
    private EditText name,nick,email,password,number,sexo,tpativo;
   private String mName,mNick,mEmail,mPassword,mNumber,mSexo,mTpativo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cadastro);

        btnfincad = (Button) findViewById(R.id.btnfincad);
        nick = (EditText) findViewById(R.id.etnick);
        email = (EditText)findViewById(R.id.Cemail);
        password = (EditText)findViewById(R.id.Cpassword);
        number = (EditText)findViewById(R.id.etnumcel);
        sexo = (EditText)findViewById(R.id.sexo);
        tpativo = (EditText)findViewById(R.id.tpativo);


        btnfincad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                btnfincad.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v) {
                        new onbuttonclickHttpPost().execute();
                        name = (EditText) findViewById(R.id.etname);
                        String mName = name.getText().toString();
                        String mNick = nick.getText().toString();
                        String mEmail = email.getText().toString();
                        String mPassword = password.getText().toString();
                        String mNumber = number.getText().toString();
                        String mSexo = sexo.getText().toString();
                        String mTpativo = tpativo.getText().toString();


                    }


                });
            }
        });
    }

            public class onbuttonclickHttpPost  extends AsyncTask<String, Void, String> {


                protected void onPreExecute() {
                }


                protected String doInBackground(String... arg0) {


                    try {


                        URL url = new URL("Api url");

                        JSONObject postDataParams = new JSONObject();
/*
                HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

                httpCon.setRequestProperty("Content-type", "application/json");
                httpCon.setRequestProperty("Accept", "application/json");
                httpCon.setRequestProperty("X-DreamFactory-Api-Key", "36fda24fe5588fa4285ac6c6c2fdfbdb6b6bc9834699774c9bf777f706d05a88");
                httpCon.setRequestProperty("X-DreamFactory-Session-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjE0LCJ1c2VyX2lkIjoxNCwiZW1haWwiOiJ0aGlhZ28uY2FtYXJnb0Bldm9sdXRpb25pdC5jb20uYnIiLCJmb3JldmVyIjpmYWxzZSwiaXNzIjoiaHR0cDpcL1wvMTkyLjE2OC4xLjIwN1wvYXBpXC92Mlwvc3lzdGVtXC9hZG1pblwvc2Vzc2lvbiIsImlhdCI6MTQ5NDE4NjA2MCwiZXhwIjoxNDk0MTg5NjYwLCJuYmYiOjE0OTQxODYwNjAsImp0aSI6ImM5N2VkMzY4MDU0MWQ5ZDI3OGJkZjAwNWYwMmMwYmY0In0.2j06THHfdJiBdwWOP1Dpkqs1Un2tSeVWa0NxVBpNxBo");
                httpCon.setRequestProperty("Authorization", "Basic  dGhpYWdvLmNhbWFyZ29AZXZvbHV0aW9uaXQuY29tLmJyOmluaWNpYWwyMDE3");
                httpCon.setRequestMethod("POST");
                httpCon.setReadTimeout(15000 *//* milliseconds *//*);
                httpCon.setConnectTimeout(15000 *//* milliseconds *//*);
                httpCon.setDoInput(true);
                httpCon.setDoOutput(true);*/


                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setRequestProperty("Content-type", "application/json");
                        conn.setRequestProperty("Accept", "application/json");
                        conn.setRequestProperty("X-DreamFactory-Api-Key", "36fda24fe5588fa4285ac6c6c2fdfbdb6b6bc9834699774c9bf777f706d05a88");
                        conn.setRequestProperty("X-DreamFactory-Session-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.fb69q5ByrWAMhanFWMpye78KN7OxgYC0IGsdoGgUYps");
                        conn.setRequestProperty("Authorization", "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
                        //conn.setRequestProperty("-d", "{}");
                        conn.setRequestMethod("POST");
                        conn.setReadTimeout(15000 /* milliseconds */);
                        conn.setConnectTimeout(15000 /* milliseconds */);
                        conn.setDoInput(true);
                        conn.setDoOutput(true);



                        postDataParams.put("email", "mName");
                        postDataParams.put("password", "mNick");
                        postDataParams.put("dt_nascimento", "mData");
                        postDataParams.put("nu_cellphone", "mNumber");
                        postDataParams.put("password", "mPassword");
                        postDataParams.put("sexo", "mSexo");
                        postDataParams.put("tp_ativo", "mTpativo");


                        Log.e("resource", postDataParams.toString());




                        //postDataParams.put("password", "password");

                        JSONObject resource = new JSONObject();
                        JSONArray array = new JSONArray();
                        array.put(postDataParams);
                        resource.put("resource", array);

                        System.out.println(resource.toString());


                        conn.connect();

                        OutputStream os = conn.getOutputStream();
                        BufferedWriter writer = new BufferedWriter(
                                new OutputStreamWriter(os, "UTF-8"));
                        //writer.write(getPostDataString(postDataParams));
                        writer.write(resource.toString());

                        writer.flush();
                        writer.close();
                        os.close();

                        int responseCode = conn.getResponseCode();

                        if (responseCode == HttpsURLConnection.HTTP_OK) {

                            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                            StringBuffer sb = new StringBuffer("");
                            String line = "";

                            while ((line = in.readLine()) != null) {

                                sb.append(line);
                                break;
                            }

                            in.close();
                            return sb.toString();
                        } else {
                            return new String("false : " + responseCode);
                        }
                    } catch (Exception e) {
                        return new String("Exception: " + e.getMessage());
                    }
                }

                @Override
                protected void onPostExecute(String result) {
                    Toast.makeText(getApplicationContext(), result,
                            Toast.LENGTH_LONG).show();

                }
            }

        }
    
asked by anonymous 12.05.2017 / 19:49

1 answer

0

Samara,

The error is occurring because you are declaring the Strings twice, and also because you are sending the server as text instead of as a variable.

Example:

You stated above:

private String mName,mNick,mEmail,mPassword,mNumber,mSexo,mTpativo;

So when you're going to use them, you do not need to put String in front of it again.

Example:

mName = name.getText().toString();
mNick = nick.getText().toString();
mEmail = email.getText().toString();
mPassword = password.getText().toString();
mNumber = number.getText().toString();
mSexo = sexo.getText().toString();
mTpativo = tpativo.getText().toString();

That way, the variables you declared above are already popular.

And finally, when you send them to the server, you do not need the quotation marks.

Example:

postDataParams.put("email", mName);
postDataParams.put("password", mNick);
postDataParams.put("dt_nascimento", mData);
postDataParams.put("nu_cellphone", mNumber);
postDataParams.put("password", mPassword);
postDataParams.put("sexo", mSexo);
postDataParams.put("tp_ativo", mTpativo);

Notice that I have taken the quotes from the second argument, which are the populated variables.

And one of the checks, because you are passing the password twice, one as mick and another as mPassword, I believe only the second one is right.

Anything screams there

    
12.05.2017 / 19:55