我有这个代码,我不能从这里获取变量字符串
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();
}
我需要获取变量字符串“passwd”到...
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());
}
}
这里 - &gt; postDataParams.put(“mensajeArea”, passwd );
帮助,我已经尝试了几个小时而没有成功......
答案 0 :(得分:0)
像这样的东西
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());
}
}
}
在构造函数的帮助下创建任务并设置passwd
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();
}
});