Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.yuliana.accessws01;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.Toast;
- import com.loopj.android.http.AsyncHttpClient;
- import com.loopj.android.http.AsyncHttpResponseHandler;
- import com.loopj.android.http.RequestParams;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- Button btnSearch,btnLogin,btnRegister;
- EditText edtUsername,edtPassword,edtNama,edtGaji;
- String tombol;
- ListView lv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnSearch = (Button) findViewById(R.id.btnSearch);
- btnLogin = (Button) findViewById(R.id.btnLogin);
- btnRegister = (Button)findViewById(R.id.btnReg);
- edtPassword = (EditText)findViewById(R.id.txtpassword);
- edtUsername = (EditText)findViewById(R.id.txtusername);
- edtNama = (EditText)findViewById(R.id.txtNama);
- edtGaji = (EditText)findViewById(R.id.txtGaji);
- lv = (ListView)findViewById(R.id.listView);
- btnRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- RequestParams param = new RequestParams();
- param.put("tusername",edtUsername.getText().toString());
- param.put("tpasswd", edtPassword.getText().toString());
- param.put("tnama",edtNama.getText().toString());
- param.put("tgaji",edtGaji.getText().toString());
- tombol = "register";
- panggilWS("post",param);
- }
- });
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- RequestParams param = new RequestParams();
- param.put("tusername",edtUsername.getText().toString());
- param.put("tpasswd", edtPassword.getText().toString());
- tombol = "login";
- panggilWS("post",param);
- }
- });
- btnSearch.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- RequestParams param = new RequestParams();
- param.put("tusername",edtUsername.getText().toString());
- tombol = "search";
- panggilWS("post",param);
- }
- });
- }
- public void panggilWS(String jenis, RequestParams param){
- AsyncHttpClient client = new AsyncHttpClient();
- if(jenis.equalsIgnoreCase("post")) {
- String command = "";
- if(tombol.equalsIgnoreCase("register"))
- { command = "http://10.10.18.224/prac01/index.php/Myservice/insertmember"; }
- else if(tombol.equalsIgnoreCase("login"))
- { command = "http://10.10.18.224/prac01/index.php/Myservice/checklogin"; }
- else if(tombol.equalsIgnoreCase("search"))
- { command = "http://10.10.18.224/prac01/index.php/Myservice/selectmember"; }
- client.post(command,param,
- new AsyncHttpResponseHandler()
- {
- @Override
- public void onSuccess(String Response)
- {
- if(tombol.equalsIgnoreCase("register")) {
- Toast.makeText(getApplicationContext(),
- "Register Sukses", Toast.LENGTH_LONG).show();
- }
- if(tombol.equalsIgnoreCase("login")) {
- if(Response.equalsIgnoreCase("invalid")) {
- Toast.makeText(getApplicationContext(),
- "Username Password Anda Salah", Toast.LENGTH_LONG).show();
- }
- else {
- Toast.makeText(getApplicationContext(),
- "Selamat Datang", Toast.LENGTH_LONG).show();
- }
- }
- if(tombol.equalsIgnoreCase("search")) {
- ArrayList<String> arr = new ArrayList<>();
- JSONArray jArray = null;
- try {
- jArray = new JSONArray(Response);
- int panjang = jArray.length();
- for(int i = 0; i < jArray.length(); i+=1)
- {
- JSONObject jsondata = jArray.getJSONObject(i);
- arr.add(jsondata.getString("username"));
- }
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,arr);
- lv.setAdapter(adapter);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void onFailure(int statusCode, Throwable error, String content)
- {
- if (statusCode == 404) {
- Toast.makeText(getApplicationContext(),
- "Requested resource not found", Toast.LENGTH_LONG).show();
- }
- else if (statusCode == 500) {
- Toast.makeText(getApplicationContext(),
- "Something went wrong at server end", Toast.LENGTH_LONG).show();
- }
- else {
- Toast.makeText(getApplicationContext(),
- "Unexpected Error occcured! Might be not be connected to Internet",
- Toast.LENGTH_LONG).show();
- }
- }
- }
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement