0

I am calling a url using this code

URL url = new URL(urlString);
                HttpURLConnection httpCon = (HttpURLConnection) url
                        .openConnection();
                httpCon.setRequestMethod("HEAD");
                httpCon.setConnectTimeout(1200000);
                httpCon.setReadTimeout(1200000);
                httpCon.setRequestMethod("GET");
                httpCon.setRequestProperty("Content-Type", "application/json");
                int responseCode = httpCon.getResponseCode();
                System.out.println(responseCode);

i am running a webservice in development server using local source code.the reponse is not coming back

note:-the service running time is 7-8 min.

11
  • You want to get Response in JSON by using API Call? Commented Apr 4, 2017 at 6:06
  • yes @user3441151 i have a api that will return json but the data is so huge so it's taking 8 mints to run i can see the service is running in server log but after finish it's not returning the values. Commented Apr 4, 2017 at 7:11
  • What you get from this API Call? Is it a JSON Array or JSON Object? Commented Apr 4, 2017 at 7:15
  • You can try my answer below. Commented Apr 4, 2017 at 7:16
  • ok thanks @user3441151 Commented Apr 4, 2017 at 7:29

1 Answer 1

1

Try this.

Imports are given below :

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.InputStream;
import java.io.InputStreamReader;

Code is given below :

URL url = new URL(urlString);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestMethod("HEAD");
httpCon.setConnectTimeout(1200000);
httpCon.setReadTimeout(1200000);
httpCon.setRequestMethod("GET");
httpCon.setRequestProperty("Content-Type", "application/json");
int responseCode = httpCon.getResponseCode();
System.out.println(responseCode);

//For getting the response in JSON
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) httpCon.getContent()));
JsonObject innerRootobj = root.getAsJsonObject();
System.out.println("innerRootobj : " + innerRootobj);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.