Volley ile Rest Api Kullanımı ( POST, GET, PUT, DELETE )

Volley ile Rest Api Kullanımı ( POST, GET, PUT, DELETE )

package com.mobilhanem.phprestapivolleyexample;

 

import android.app.ProgressDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

 

import com.android.volley.AuthFailureError;

import com.android.volley.DefaultRetryPolicy;

import com.android.volley.NetworkResponse;

import com.android.volley.Request;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.JsonObjectRequest;

import com.android.volley.toolbox.StringRequest;

 

import org.json.JSONException;

import org.json.JSONObject;

 

import java.util.HashMap;

import java.util.Map;

 

public class GetListUser extends AppCompatActivity {

 

    private Button bttn;

    private ProgressDialog progress;

    private EditText userIdInput;

    private TextView txtId, txtNameSurname, txtUserName, txtEmail, txtPassword, txtPhone;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_get_list_user);

 

        getSupportActionBar().setTitle(“GET İSTEĞİ”);

 

        bttn = (Button)findViewById(R.id.button);

        txtId = (TextView) findViewById(R.id.userId);

        txtNameSurname = (TextView)findViewById(R.id.nameSurname);

        txtUserName = (TextView)findViewById(R.id.userName);

        txtEmail = (TextView)findViewById(R.id.userEmail);

        txtPassword = (TextView)findViewById(R.id.userPassword);

        txtPhone = (TextView)findViewById(R.id.userPhone);

        userIdInput = (EditText)findViewById(R.id.userIdText);

 

 

        bttn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

 

                if(userIdInput.getText().length()>0){

                    clearUserInfos();

                    executeGetMethod();

                }else{

                    Toast.makeText(getApplicationContext(),“Lütfen Bilgilerini Getirmek İstediğiniz Kullanıcının ID sini giriniz!”,Toast.LENGTH_SHORT).show();

                }

            }

        });

    }

 

    private void clearUserInfos() {

 

        txtId.setText(“”);

        txtNameSurname.setText(“”);

        txtUserName.setText(“”);

        txtEmail.setText(“”);

        txtPassword.setText(“”);

        txtPhone.setText(“”);

    }

 

    private void executeGetMethod() {

 

        progress = ProgressDialog.show(GetListUser.this,“”,

                “Lütfen Bekleyiniz”, true);

 

 

        int userID = Integer.parseInt(userIdInput.getText().toString());

 

        StringRequest jsonForGetRequest = new StringRequest(

                Request.Method.GET,“http://vehbiakdogan.com/mobilhanem/index.php?user_id=” + userID,

                new Response.Listener() {

                    @Override

                    public void onResponse(String response) {

 

                        progress.dismiss();

                        Log.i(“log”,response.toString());

                        try {

                            JSONObject obj = new JSONObject(response);

                            JSONObject jsonBody = obj.getJSONObject(“uye-bilgileri”);

                            txtId.setText(jsonBody.getString(“id”));

                            txtUserName.setText(jsonBody.getString(“kullaniciAdi”));

                            txtNameSurname.setText(jsonBody.getString(“adSoyad”));

                            txtPassword.setText(jsonBody.getString(“sifre”));

                            txtEmail.setText(jsonBody.getString(“posta”));

                            txtPhone.setText(jsonBody.getString(“telefon”));

 

                        } catch (JSONException e) {

                            e.printStackTrace();

                        }

 

 

                    }

 

 

                }, new Response.ErrorListener() {

 

            @Override

            public void onErrorResponse(VolleyError error) {

 

                progress.dismiss();

 

                NetworkResponse response = error.networkResponse;

                if(response != null && response.data != null){

                    JSONObject jsonObject = null;

                    String errorMessage = null;

 

                    switch(response.statusCode){

                        case 400:

                            errorMessage = new String(response.data);

 

                            try {

 

                                jsonObject = new JSONObject(errorMessage);

                                String serverResponseMessage =  (String)jsonObject.get(“hataMesaj”);

                                Toast.makeText(getApplicationContext(),“”+serverResponseMessage,Toast.LENGTH_LONG).show();

 

 

                            } catch (JSONException e) {

                                e.printStackTrace();

                            }

                    }

                }

            }

 

 

        }) {

 

            @Override

            public Map<String, String> getHeaders() throws AuthFailureError {

 

                Map<String, String> param = new HashMap<String, String>();

 

                return param;

            }

 

 

        };

 

        jsonForGetRequest.setRetryPolicy(new DefaultRetryPolicy(10000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

 

        AppController.getInstance().addToRequestQueue(jsonForGetRequest);

 

    }

}

Yorum Yap
0 Yorum yapan