Jose Torres Jose Torres - 20 days ago 8
Android Question

variable textview not working

i have this code and i cant get the variable string from here

private void location() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.alquila, null);
TextView tv = (TextView) promptView.findViewById(R.id.gen);
Random r = new Random();
int i = r.nextInt(101);
tv.setText(i +"");
final String passwd = tv.getText().toString();
final AlertDialog alertD = new AlertDialog.Builder(this).create();

Button btnAdd1 = (Button) promptView.findViewById(R.id.button3);


btnAdd1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new SendPostRequest().execute();
Toast.makeText(login.this, passwd, Toast.LENGTH_LONG).show();
// btnAdd1 has been clicked
alertD.dismiss();
}
});

alertD.setView(promptView);

alertD.show();
}


And i need get the variable string "passwd" to...

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

protected void onPreExecute(){}

protected String doInBackground(String... arg0) {

try {

URL url = new URL("https://www.domain.org/file.php"); // here is

JSONObject postDataParams = new JSONObject();
postDataParams.put("mensajeArea", passwd);
Log.e("params",postDataParams.toString());

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

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

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

}


here --> postDataParams.put("mensajeArea", passwd);

help, I've been trying for hours without success...

Answer

Something like that

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

    private String passwd;
    public SendPostRequest(String pass) {
        super();
        this.passwd = pass;
    }

    protected void onPreExecute() {
    }

    protected String doInBackground(String... arg0) {

        try {

            URL url = new URL("https://www.domain.org/file.php"); // here is

            JSONObject postDataParams = new JSONObject();
            postDataParams.put("mensajeArea", passwd);
            Log.e("params", postDataParams.toString());

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

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

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

    }
}

Create task with the help of constructor and set passwd

In onclick:

btnAdd1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        new SendPostRequest(passwd).execute();
        Toast.makeText(login.this, passwd, Toast.LENGTH_LONG).show();
        // btnAdd1 has been clicked
        alertD.dismiss();
    }
});
Comments