Pencarian

Kamis, 26 Desember 2013

Android Client Send and Receive Socket


Sudah lama tidak nulis, sebagai catatan dihari tua:
Bagaimana cara client mengirim data dan menerima hasil dari server menggunakan socket programming.

package com.example.clientserver;import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity{
private EditText txtip, txtport, txtpesan;
private Button btnKirim;
private String inPesan="";
private TextView txtresult;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtip = (EditText) findViewById(R.id.txtip);
        txtport = (EditText) findViewById(R.id.txtport);
        txtpesan = (EditText) findViewById(R.id.txtpesan);
        txtresult  = (TextView)  findViewById(R.id.txtrespone);
        btnKirim = (Button) findViewById(R.id.btnKirim);   //reference to the send button      
    txtresult.setText(inPesan);         btnKirim.setOnClickListener(new View.OnClickListener(){    
      public void onClick(View v) {
     new tcpClient().execute("");
    }
     });    
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }  
   private class tcpClient extends AsyncTask{
String hostname = String.valueOf(txtip.getText());
int    port     = Integer.valueOf(String.valueOf(txtport.getText()));
@Override
protected String doInBackground(String... params) {
 String sentence;
      String modifiedSentence = "";
      BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
      Socket clientSocket;
try {
clientSocket = new Socket(hostname,port);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
   BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
   sentence = String.valueOf(txtpesan.getText());
   outToServer.writeBytes(sentence + '\n');
   modifiedSentence = inFromServer.readLine();
   System.out.println("FROM SERVER: " +modifiedSentence);
   clientSocket.close();
} catch (UnknownHostException e) {
modifiedSentence = "Server error HOST";
e.printStackTrace();
} catch (IOException e) {
modifiedSentence = "Server error IO";
e.printStackTrace();
}    
return modifiedSentence.toString();
}
@Override
protected void onPostExecute(String result) { super.onPostExecute(result);
txtresult.setText(result);
txtpesan.requestFocus();
}    }
}
Inti dari kode diatas, agar android bisa send and receive data, kita bisa menggunakan Class AsyncTask dan mengimplementasikan method doInBackground() dan  onPostExecute()

Tidak ada komentar:

Posting Komentar