2016-11-12 61 views
0

我有這樣的代碼,我不能從這裏變量的TextView不工作

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

    } 

在這裏 - - > postDataParams.put(「mensajeArea」,passwd);

的幫助,我一直在努力爲沒有成功小時......

+0

你不能這樣做。因爲SendPostRequest在位置方法 – Pein

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

它的工作!大!!你救了我的一天! –