Mohsin Arif

Mohsin Arif

  • 295
  • 6k
  • 137.8k

how can i call api without key in Volley ?

Mar 10 2020 11:59 AM
hellow guys below is my code, i want to get data from api and populate in graph in android but when i call api its shows error please help me, how can i call , api which have no any key 
  1. package com.example.myapplication;  
  2.   
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6. import android.widget.TextView;  
  7.   
  8. import androidx.appcompat.app.AppCompatActivity;  
  9.   
  10. import com.android.volley.Request;  
  11. import com.android.volley.RequestQueue;  
  12. import com.android.volley.Response;  
  13. import com.android.volley.VolleyError;  
  14. import com.android.volley.toolbox.JsonArrayRequest;  
  15. import com.android.volley.toolbox.JsonObjectRequest;  
  16. import com.android.volley.toolbox.Volley;  
  17.   
  18. import org.json.JSONArray;  
  19. import org.json.JSONException;  
  20. import org.json.JSONObject;  
  21.   
  22. public class MainActivity extends AppCompatActivity {  
  23.     private TextView mTextViewResult;  
  24.     private RequestQueue mQueue;  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.   
  31.         mTextViewResult = findViewById(R.id.text_view_result);  
  32.         Button buttonParse = findViewById(R.id.button_parse);  
  33.   
  34.         mQueue = Volley.newRequestQueue(this);  
  35.   
  36.         buttonParse.setOnClickListener(new View.OnClickListener() {  
  37.             @Override  
  38.             public void onClick(View v) {  
  39.                 jsonParse();  
  40.   
  41.             }  
  42.         });  
  43.     }  
  44.   
  45.     private void jsonParse() {  
  46.   
  47.         String url = "http://marjanjewellers.co/bilal/api.php";  
  48.   
  49.         JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,  
  50.                 new Response.Listener<JSONObject>() {  
  51.                     @Override  
  52.                     public void onResponse(JSONObject response) {  
  53.                         try {  
  54.   
  55.   
  56.                             JSONArray jsonArray = response.getJSONArray("0"); //here what i do 
  57.   
  58.                             for (int i = 0; i < jsonArray.length(); i++) {  
  59.                                 JSONObject employee = jsonArray.getJSONObject(i);  
  60.   
  61.                                 //String firstName = employee.getString("firstname");  
  62.                                 //int age = employee.getInt("age");  
  63.                                // String mail = employee.getString("mail");  
  64.   
  65.                               //  mTextViewResult.append(firstName + ", " + String.valueOf(age) + ", " + mail + "\n\n");  
  66.                             }  
  67.                         } catch (JSONException e) {  
  68.                             e.printStackTrace();  
  69.                         }  
  70.                     }  
  71.                 }, new Response.ErrorListener() {  
  72.             @Override  
  73.             public void onErrorResponse(VolleyError error) {  
  74.                 error.printStackTrace();  
  75.             }  
  76.         });  
  77.   
  78.         mQueue.add(request);  
  79.     }  
  80.   
  81. }  
 and api response is like this 
 
  1. [{"id":"1","altitude":"95.5","airpressure":"98.45","temperature":"45.89","pulserate":"85"}]  
 

Answers (1)