Post via Json getstring error

Asked

Viewed 52 times

0

I have a code that should make a "Post" via json ,but edittext is not converting to String,and there is no error in logcat .. Anyone have any idea of the mistake? It must be something stupid I apologize,.

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();

                }
            }

        }

1 answer

0

Samara,

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

Example:

You stated up there:

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

So when you use them, you don’t need to put String in front 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 you’ll already popular the variables you stated up there.

And finally, when sending them to the server, you don’t need the quotes.

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 removed the quotation marks from the second argument, which are the populated variables.

And give it a check, because you’re going twice to "password", one as mNick and another as mPassword, I believe only the second is right.

Something screams in there

  • Thank you very much !! It worked !! + 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.