2016-07-12 22 views
-1

私はAndroidにはとても新しいので、ここや他のウェブサイトから多くの投稿に記載されているソリューションを試してみてください。 Androidのアクティビティ(ボタンをクリックしたとき)から2つの変数を渡したいと思います。Androidからphpに変数を送信するだけです

私が試みているのは、アクティビティからPHPページに変数を渡すことです。 これはシミュレータ上で動作しますか?または、私はそれが動作確認するためにアプリをリリースする必要がありますか? "onclickの"

しかし、と

public class Main3Activity extends AppCompatActivity { 

public String num_pal, precio, l_origen, l_destino, texto, banner_id, full_id; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main3); 

    Intent intent=getIntent(); 
    double precio = intent.getExtras().getDouble("precio"); 
    String l_origen = intent.getExtras().getString("l_origen"); 
    String l_destino = intent.getExtras().getString("l_destino"); 
    String texto= intent.getExtras().getString("texto"); 
    int pal = intent.getExtras().getInt("num_pal"); 
    num_pal = String.valueOf(pal); 
    String precio_rounded = String.format("%.2f", precio); 
    TextView txtCambio = (TextView) findViewById(R.id.textView4); 
    txtCambio.setText("Precio Total: "+ precio_rounded + " €"); 
} 

void MakePostRequest() { 
     String posting_url ="https://www.example.org/movil/ingles/page.php"; 

     StringRequest postRequest = new StringRequest(Request.Method.POST, posting_url , 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject jsonResponse = new JSONObject(response); 
         precio= jsonResponse.getString("precio_rounded"); 
         l_origen= jsonResponse.getString("l_origen"); 
         l_destino= jsonResponse.getString("l_destino"); 
         texto= jsonResponse.getString("texto"); 
         num_pal= jsonResponse.getString("num_pal"); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
         banner_id = null; 
         full_id = null; 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        error.printStackTrace(); 
        precio= null; 
        l_origen= null; 
        l_destino= null; 
        texto= null; 
       } 
      } 
    ) { 
     // here is params will add to your url using post method 
     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<>(); 
      params.put("precio", precio); 
      params.put("l_origen", l_origen); 
      params.put("l_destino", l_destino); 
      params.put("texto", texto); 
      params.put("num_pal", num_pal); 
      //params.put("2ndParamName","valueoF2ndParam"); 
      return params; 
     } 
    }; 
    Volley.newRequestQueue(this).add(postRequest); 
} 

そしてボタン:

は、私は、次のコード(Sohail Zahidのおかげで)持って、今

ではお時間をいただき、ありがとうございますこのボタンをクリックするとアプリケーションが終了し、エラーが報告されます。

誰にも理由がありますか?

+0

また、私は画面でそれらを示すよりも、これらの変数で何かを行うことができないことを読んで、私は彼らと「再生」したい場合は、私がする必要がありますそれらをデータベースに格納する。本当?それはどういう意味ですか? – Whencesoever

+0

例えば、それらの変数を他のPHPページに再送したいのであれば、それらをデータベースに保存する必要がありますか?あなたの答えをありがとう – alberzyzz

+0

それは本当ではないです。 Androidから最初のPHPスクリプトに変数を送信するとしましょう。それを操作する。操作された変数は、別のPHPに送信されます。あなたはそれが同じ目的ではないことを知る必要があります。あなたは値を渡すことができます。名前による変数ではありません。 – Whencesoever

答えて

1

あなたはポストを使用するか、PHP

そして、最後の質問にURLで変数を送信するために取得することができます。また、私はそれらを示すよりも、これらの変数で何かを行うことができないことを読みます私はそれらを "再生"したい場合、私はそれらをデータベースに格納する必要があります。本当?

は、データベースに格納したり、ここにポストメソッドを使用してPHPスクリプトにパラメータ/値を送信後に

1

それを使用するために、グローバル変数に割り当てることができる簡単な例です。

Post Request

void MakePostRequest() { 
      String posting_url ="http://webIpaddress/android/ads/ids.php"; 
      // its your url path ok 
      StringRequest postRequest = new StringRequest(Request.Method.POST, posting_url , 
        new Response.Listener<String>() { 
         @Override 
         public void onResponse(String response) { 
          try { 
           JSONObject jsonResponse = new JSONObject(response); 
           String value1= jsonResponse.getString("Your ID1"); 
           String value2= jsonResponse.getString("Your ID2"); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
           banner_id = null; 
           full_id = null; 
          } 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
          error.printStackTrace(); 
          String value1= null; 
          String value2= null; 
         } 
        } 
      ) { 
      // here is params will add to your url using post method 
       @Override 
       protected Map<String, String> getParams() { 
        Map<String, String> params = new HashMap<>(); 
        params.put("app","sendingValuesHere"); 
        //params.put("2ndParamName","valueoF2ndParam"); 
        return params; 
       } 
      }; 
      Volley.newRequestQueue(this).add(postRequest); 
     } 
+0

ありがとうございますが、 "記号 'StringRequest'を解決できません"のようなエラーがあります – alberzyzz

+0

これをあなたのアプリのgradleファイル 'compile 'に追加してください。com.mcxiaoke.volley:library:1.0.19'' –

+0

" StringRequest " EndPoints、Listener、ErrorListener、Volley – alberzyzz

関連する問題