Mustafa Bekeç
Mustafa Bekeç

Reputation: 71

How to set header in Android Json

I want to fetch data from the Web service HttpGet Method. Web Service needs two header information.

How can i add set Parameters ?

"Content-Type", "application/json"
"Authorization", "Bearer " + Utils.PreferencesGetir(ActivityPuanlarim.this, "Preferences_Token"))

Json Data

[  
{
"ID": 12,
"KayitTarihi": "2016-07-21T08:37:01.603",
"KullanilanPuan": 0,
"KuponID": 2,
"KuponKullanim": [],
"KuponNo": "S7240061",
"KuponAciklama": "Açıklama 1",
"Puan": 40
},
{
"ID": 13,
"KayitTarihi": "2016-07-21T09:38:48.877",
"KullanilanPuan": 0,
"KuponID": 2,
"KuponKullanim": [],
"KuponNo": "S7240071",
"KuponAciklama": "Açıklama 2",
"Puan": 40 
 }
]

AsyncTask doInBackground

 @Override
    protected Void doInBackground(Void... voids) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("Content-Type", "application/json"));
        params.add(new BasicNameValuePair("Authorization", "Bearer " + Utils.PreferencesGetir(ActivityPuanlarim.this, "Preferences_Token")));


        JSONArray json = jParser.makeHttpRequestArray(URL, "GET", params);

        try {

            if (Utils.InternetKontrol(ActivityPuanlarim.this) == true) {
                for (int i = 0; i < json.length(); i++) {
                    JSONObject c = json.getJSONObject(i);


                    String id = c.getString("ID");
                    String kayitTarihi = c.getString("KayitTarihi");
                    String kullanilanPuan = c.getString("KullanilanPuan");
                    String kuponID = c.getString("KuponID");
                    String kuponKullanim = c.getString("KuponKullanim");
                    String kuponNo = c.getString("KuponNo");
                    String kuponAciklama = c.getString("KuponAciklama");
                    String puan = c.getString("Puan");


                    // Hashmap oluşturulur
                    HashMap<String, String> map = new HashMap<String, String>();


                    map.put("ID", id);
                    map.put("KayitTarihi", kayitTarihi);
                    map.put("KullanilanPuan", kullanilanPuan);
                    map.put("KuponID", kuponID);
                    map.put("KuponKullanim", kuponKullanim);
                    map.put("KuponNo", kuponNo);
                    map.put("KuponAciklama", kuponAciklama);
                    map.put("Puan", puan);

                    puanlarimTumKayitlarList.add(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

Upvotes: 1

Views: 1361

Answers (1)

BuckRogers
BuckRogers

Reputation: 13

As you do not send a HttpRequest, you don't need these parameters. You should rather request content from Servlet via URLConnection and then read the data in your activity.

Example here: https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html

Edit: I've read that you can use HttpGet in Activity like this:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("key", VALUE);
HttpResponse httpResponse = httpClient.execute(httpGet);

Upvotes: 1

Related Questions