-2

I want to fetch the json data from web api, I was previously using Retrofit for that, but I don't wanna use any third party library.

I know I can use HttpURLConnection or HttpClient but there is no proper post for that and they are too old, and in some post they were telling that it is deprecated, so if you have any other solution using HttpUrlConnection and HttpClient or without using that then please let me know.

And please tell me how to parse that data cause before that I was using GSONParser library for that.

Here is my example api:

https://www.mocky.io/v2/5b8126543400005b00ecb2fe

prasad_21
  • 117
  • 2
  • 18
  • 1
    "There is no proper post for that and they are too old" that's the price to pay if you don't want to use third-party libraries .. Why wouldn't you use them ? Also, [this post](https://stackoverflow.com/a/4457526/7540393) seems very fine to me. – Arthur Attout Dec 02 '18 at 20:33
  • Truth to be told, I have been asked several times that can you access data without any third party library in several interviews, so that's why I am asking... – prasad_21 Dec 02 '18 at 20:38
  • 1
    @prasad_21 HttpURLConnection is fine to use. You could use JsonReader to read and parse those json. – Archie G. Quiñones Dec 07 '18 at 09:43
  • yeah I will use that, but right now I am not able to retrieve data using it... – prasad_21 Dec 08 '18 at 13:51

1 Answers1

4

Hey you can retrieve your data using your methods, that depends on you. For example, I have never used a third party library to retrieve data from the server.

Think about this: a class that I can name FileContentReader with a method getContentFromUrl this will fetch your JSON data as string that you can then parse using JSONObject or JSONArray according to your file structure.

public class FileContentReader {
private Context appContext;

    public FileContentReader(Context context){
        this.appContext=context;
    }
    public String getContentFromUrl(String url)
    {
        StringBuilder content = new StringBuilder();
        try {
            URL u = new URL(url);
            HttpURLConnection uc = (HttpURLConnection) u.openConnection();
            if (uc.getResponseCode()==HttpURLConnection.HTTP_OK) {

                InputStream is = uc.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                String line;
                while ((line = br.readLine()) != null) {

                    content.append(line).append("\n");

                }

            }else{

                throw new IOException(uc.getResponseMessage());
            }
        } catch(StackOverflowError | Exception s){
                s.printStackTrace();
            } catch(Error e){
                e.printStackTrace();
            }


            return content.toString();


    }
}

You can use the code this way inside an asynchronous task or any background task :

FileContentReader fcr= new FileContentReader(getApplicationContext());

String data= fcr.getContentFromUrl("myurl");

if(!data.isEmpty())
{
try{
JSONArray ja = new JSONArray(data);
//... ou can then access and manupilate your data the way you want
}catch(JSONException e)
{
e.printStackTrace();}

}
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30
  • I tried using this but I received the exception "**java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.**" on `uc.getResponseCode()`. – prasad_21 Dec 05 '18 at 09:02
  • which protocol are you using? HTTP or HTTPS? – Gratien Asimbahwe Dec 05 '18 at 12:10
  • 1
    read about that error here https://stackoverflow.com/questions/6825226/trust-anchor-not-found-for-android-ssl-connection – Gratien Asimbahwe Dec 05 '18 at 12:31
  • I saw that post earlier, they are asking us to enter specific command at server, I am using only **mock api** from [mocky.io](https://www.mocky.io/) to get **json response**, that's why I am not sure about how to use it.... – prasad_21 Dec 06 '18 at 12:27