Mengirim Parameter Ke Server

Sep. 07, 2010

Posted by Dias Nurul Arifin under Android
0 Comments | Permalink

<p>Dalam aplikasi berbasis client server, harus di butuhkan sinkronisasi antara client dengan server, nah&#160; disini saya akan membahas bagaimana cara mengirimkan sebuah data/parameter ke server.</p>

<p>Langsung saja ke contoh permasalahan, </p>

<p>Contoh Di sebuah halaman web kita memiliki form dengan action save.action dan method adalah POST, kita akan mengirimkan parameter parameter ke save.action selayaknya di html tetapi ini mengirim dari android application.</p>

<pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 463px; padding-top: 5px; border-bottom: #cecece 1px solid; height: 222px; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">HttpPost httpPost = <span style="color: #0000ff">new</span> HttpPost(dbManager.getConnectionUrl()+&quot;<span style="color: #8b0000">Cimande2/person/</span>&quot;+person.getId()+&quot;<span style="color: #8b0000">/</span>&quot;);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff">DefaultHttpClient httpClient = <span style="color: #0000ff">new</span> DefaultHttpClient();
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb"><span style="color: #0000ff">try</span> {
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff">  List&lt;NameValuePair&gt; nameValuePairs = <span style="color: #0000ff">new</span> ArrayList&lt;NameValuePair&gt;();
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">  nameValuePairs.add(<span style="color: #0000ff">new</span> BasicNameValuePair(&quot;<span style="color: #8b0000">firstName</span>&quot;,firstName.getText().<span style="color: #0000ff">toString</span>()));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff">  nameValuePairs.add(<span style="color: #0000ff">new</span> BasicNameValuePair(&quot;<span style="color: #8b0000">lastName</span>&quot;, lastName.getText().<span style="color: #0000ff">toString</span>()));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">  httpPost.setEntity(<span style="color: #0000ff">new</span> UrlEncodedFormEntity(nameValuePairs));
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff">  HttpResponse response = httpClient.execute(httpPost);
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">} <span style="color: #0000ff">catch</span> (ClientProtocolException e) {  
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff">  e.printStackTrace();
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">} <span style="color: #0000ff">catch</span> (IOException e) {  
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff">  e.printStackTrace();
</pre><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">}</pre></pre>

<p>Di sini kita menggunakan HttpPost untuk mengirim/post data ke server, cara ini sama dengan yang biasanya kita gunakan di form html dengan form method post.</p>

<p>untuk mengelompokkan parameter? yang akan di kirim di sini kita menggunakan NameValuePair.</p>

<pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 467px; padding-top: 5px; border-bottom: #cecece 1px solid; height: 39px; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">nameValuePairs.add(<span style="color: #0000ff">new</span> BasicNameValuePair(&quot;<span style="color: #8b0000">firstName</span>&quot;,firstName.getText().toString()));</pre></pre>

<p>Nah contoh kode di atas untuk menambahkan parameter, kode di atas sama dengan kode html input</p>

<pre style="border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 463px; padding-top: 5px; border-bottom: #cecece 1px solid; height: 24px; background-color: #fbfbfb"><pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb">&lt;<span style="color: #800000">input</span> <span style="color: #800000">type</span>=&quot;<span style="color: #800000">text</span>&quot; <span style="color: #800000">name</span>=&quot;<span style="color: #800000">firstName</span>&quot;&gt;</pre></pre>

<p>Yang terakhir untuk mengirimkan semua data yang telah kita kumpulkan, di sini menggunakan method dari HttpClient yaitu execute() ini sama dengan men-submit form yang dalam versi web.</p>

<p>Cukup mudah bukan, silakan mencoba :)</p>



Post a Comment:
  • HTML Syntax: Allowed