0

im am currently creating a CMS applicationa and i am trying to create a JSON object that i can post to my API but i have no idea on how to do this because im new to android. does anyone have an Idea?

How it looks

My code:

String URL = "http://test.soundwave.drieo.nl/api/content/" + uid + "?apikey=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";

    try {
        APIClientJSONObject api = new APIClientJSONObject();
        JSONObject result = null;

        try {
            result = api.execute(URL).get();
        } catch (Exception e) {
            e.printStackTrace();
        }

            try {

                String content = result.optString("FormattedName");
                String content2 = result.optString("Title");
                String content3 = result.optString("Subtitle");
                String content4 = result.optString("Text");

                EditText name = (EditText) findViewById(R.id.etInternNaam);
                name.setText(content);
                EditText titel = (EditText) findViewById(R.id.etName);
                titel.setText(content2);
                EditText ondertitel = (EditText) findViewById(R.id.etOndertitel);
                ondertitel.setText(content3);
                EditText EditText = (EditText) findViewById(R.id.etTekst);
                EditText.setText(Html.fromHtml(content4));

                if("null" == content) {
                    name.setText("");
                }
                if("null" == content2) {
                    titel.setText("");
                }
                if("null" == content3) {
                    ondertitel.setText("");
                }
                if("null" == content4) {
                    EditText.setText("");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

API code:

package nl.drieo.soundwave.test.cms;

import android.os.AsyncTask;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;

/**
 * Created by r.devries on 14-3-2016.
*/
public class APIClientJSONObject extends AsyncTask<String, Void, JSONObject>           {

@Override
protected JSONObject doInBackground(String... params) {

    JSONObject result = null;

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0]));
        InputStream inputStream = httpResponse.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder builder = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        result = new JSONObject(builder.toString());
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

}

1 Answers1

0

you can create JSONObject like this and put your data into that:

JSONObject json = new JSONObject();
json.put("user", "example");

after putting your data to json, you can pass the json to the 'APIClientJSONObject' class by this way:
EDIT: use this code in to OnClickListener for your Button

try {
        result = api.execute(URL,json.toString()).get();
    } catch (Exception e) {
        e.printStackTrace();
    }

and you can get Json in 'APIClientJSONObject' class like this:

JSONObject jsonObject = new JSONObject(params[1])

or if you want 'POST' this json to the webservice, you can use:
EDIT: use this in to the APIClientJSONObject class in doInBackground method:

HttpPost httpost = new HttpPost(params[0]);
StringEntity stringentity = new StringEntity(params[1]);

httpost.setEntity(stringentity);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
httpclient.execute(httpost, responseHandler);

I hope these code are useful for you.

Omid Zamani
  • 414
  • 1
  • 3
  • 15
  • Thank you for your response, im gonna be trying this out now, what do you mean with example? the text has to go into user? –  Mar 15 '16 at 08:08
  • the value of "user" is " example" please read [this](http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html) for more information about JSONObject – Omid Zamani Mar 15 '16 at 08:15
  • Okay thank you, now that i have done that, my code doesnt recognize params? is it something else that has to be filled in here? –  Mar 15 '16 at 08:38
  • it doesnt know httpclient for some reason and i dont know how to declare the right one. I also get the error on: StringEntity stringentity = new StringEntity(params[1]); unhandled exception. Do i have to make this a try and catch? Also i dont fully understand what you meant with this: JSONObject jsonObject = new JSONObject(params[1]) where do i put this? in my main activity or my API? –  Mar 15 '16 at 09:17
  • set codes in tyr catch. *JSONObject jsonObject = new JSONObject(params[1])* is just for understanding how you can cast String to Json. for HttpClient you need to add this code in your gradle, in to the android method: *useLibrary 'org.apache.http.legacy'* – Omid Zamani Mar 15 '16 at 09:24
  • Warning:Unable to find optional library: org.apache.http.legacy this is what i get when i add the library. –  Mar 15 '16 at 09:30