2016-11-12 2 views
0

ich diesen Code haben und ich kann die variable Zeichenfolge von hiervariable Textview funktioniert nicht

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

Und ich brauche erhalten die Variable string "passwd" zu ...

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

    } 

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

Hilfe, ich habe ohne Erfolg stundenlang versucht, ...

+0

Sie können es nicht tun. Weil SendPostRequest nichts über die lokale Variable in der Location-Methode weiß – Pein

+0

Wie könnte ich tun? –

Antwort

0

etwas wie die

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

    } 
} 

Aufgabe erstellen mit Hilfe von Konstruktor und setze 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(); 
    } 
}); 
+0

es ist Arbeit! groß!! du hast meinen Tag gerettet!!! –

Verwandte Themen