POST Request Java
I've read this post on how to do a POST request in Java. I don't understand how I can implement this in a JSON parser. This is what I tried so far:
public class JSONParser {
private String read(BufferedReader bufferedReader) throws IOException {
//Creates new StringBuilder to avoid escaping chars
StringBuilder stringBuilder = new StringBuilder();
//Gets the currentLine
String currentLine;
while((currentLine = bufferedReader.readLine()) !=null ){
//Adds the currentLine to the stringBuild if the currentLine is not null
stringBuilder.append(currentLine);
}
//Returns the StringBuilder is String format
return stringBuilder.toString();
}
public JSONObject readJsonFromUrl(String JSONurl) throws IOException, JSONException {
InputStream url = new URL(JSONurl).openStream();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url));
String jsonText = read(bufferedReader);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
url.close();
}
}
public void printJSON() throws IOException, JSONException {
JSONObject json = readJsonFromUrl("http://grwn.ddns.net:1337/locations");
System.out.print(json);
//for (Integer i = 0; i < json.getJSONArray("damage_or_theft_car").length(); i++) {
// System.out.println(json.getJSONArray("damage_or_theft_car")
//.getJSONObject(i).get("hood_id"));
//}
}
}
When I run this code with a link which doesn't require a POST request it all works fine but when I run this code on a link which DOES require a POST request I get the following error:
Exception in thread "main" java.io.FileNotFoundException: http://grwn.ddns.net:1337/locations
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1872)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.URL.openStream(URL.java:1045)
at com.company.JSONParser.readJsonFromUrl(JSONParser.java:30)
at com.company.JSONParser.printJSON(JSONParser.java:42)
at com.company.Main.main(Main.java:33)
Could someone help me out or point me in the right direction?
I think you need to specify that you want to make a POST request after you open the connection, maybe try something like this.
public JSONObject readJsonFromUrl(String JSONurl) throws IOException, JSONException {
URL url = new URL(JSONurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProprtry("Content-type", "application/JSON");
try {
BufferedReader bufferedReader = new BufferedReader(conn. getInputStream());
String jsonText = read(bufferedReader);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
url.close();
}
}
Possible causes for this issue might be one of the following;
1) Nothing to read/fetch from url
2) Check proxy settings, might be something configured on proxy for access to above url
3) Hostname mapping missing on your host
Some of the ways to verify this manually are as follows;
1) Try to access url from the web browser with proxy setting, see if you can get the desired raw json
2) Try to access url from the web browser without proxy setting, see if you can get the desired raw json
3) If step 1) or Step2) is successful, try same from your java code
Related
I want to retrieve with Java 1.6 an access token by usinig HTTP method GET, client Id and secret.
I am using the below code to make it work, but the response i receive is a generic html page instead of the json response with the tokens in it.
Can you please check the code for any mistakes I have made?
Thank you in advance.
private String setAccessTokenByClientIdAndSecret() throws Exception {
String accessToken = "";
try {
StringBuilder sb = null;
sb = new StringBuilder("Grant_type=client_credentials");
sb.append("&Client_Id=").append(msdOauthClientId).append("&Client_secret=").append(msdOauthSecret).append("&Resource=").append(msdOauthResourceUrl);
URL url = new URL(msdOathUrl+"?"+sb.toString());
HttpURLConnection endPointRequestConnection = (HttpURLConnection) url.openConnection();
endPointRequestConnection.setRequestMethod("GET");
endPointRequestConnection.setRequestProperty("Content-Type", "multipart/form-data");
endPointRequestConnection.setInstanceFollowRedirects(false);
if (endPointRequestConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new Exception("Error in obtaining an access token. " + endPointRequestConnection.getResponseMessage());
}
InputStream tokenRequestStream = endPointRequestConnection.getInputStream();
JSONObject tokenRequestResult = new JSONObject(tokenRequestStream);
accessToken = (String) tokenRequestResult.get("access_token");
} catch (Exception e) {
}return accessToken;
}
I've been trying to figure out how to handle the json response from a GET request. This is the code I have for the request:
String urlString = URL I want to request;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
String response = getStringFromInputStream(is);
My question is what to do now. I've been playing with gson but I cannot figure out how to parse the response for the required information.
I also found this method somewhere online to turn the response into a string:
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
If anyone could help me parse the string or somehow otherwise handle the response I'd really appreciate it.
You can use Jackson framework to parse the response into Java Object. Once you add the dependency, this is how it will work:
Let's say you are getting the below response from the API:
{
"id": 1,
"name": "foo"
}
You can write the following code to deserialise it into an Object:
public class Response {
private int id;
private String name;
//Getters and Setters
}
//code to convert
String response = sb.toString();
ObjectMapper mapper = new ObjectMapper();
Response responseObject = mapper.readValue(response, Response.class);
If the json is not valid, it will fail and throw JsonParseException or JsonMappingException. You can handle it in your code and respond accordingly.
Here's step by step tutorial on how to use Jackson.
It depends on the what you want to do with Json.I am assuming you want to convert in into an Java object.
For that you can use Gson or Jackson.
Suppose you have a json like
{"userId":"1","userName":"Yasir"}
and your java class like
class User{
int userId;
String userName;
//setters and getters
}
then you can use something like
Gson gson = new Gson();
User user= gson.fromJson(jsonInString, User.class);
to create java object.
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
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();}
}
I am developing android app where I am getting web-service data as:
"[{\"ID\":51,\"Text\":\"!! SAMPLE PROJECT !!\"},{\"ID\":58,\"Text\":\"01 Contracting Test Project\"},{\"ID\":64,\"Text\":\"1212\"},{\"ID\":45,\"Text\":\"CHEMICAL FACTORY PROJECT\"}]"
Now I want to parse this data in json I used replaceAll() function to replace backslashes from the string like this:
String jsonFormattedString = line.replaceAll("\\\\", "");
But I think this method isnot good to work with because it removes all the backslashes from the string which creates problems like I recieved json node like:
"[{\"ID\":9617,\"Text\":\"1 1\/4\\\" PVC\/GI CLAMPS\"}]"
where the string value for Text contains double quotes within string which creates problem for me. So my question is what is the best way to parse this json data in java.
My full json data returned by webservice is as:
"[{\"ID\":51,\"Text\":\"!! SAMPLE PROJECT !!\"},{\"ID\":58,\"Text\":\"01 Contracting Test Project\"},{\"ID\":64,\"Text\":\"1212\"},{\"ID\":45,\"Text\":\"CHEMICAL FACTORY PROJECT\"},{\"ID\":53,\"Text\":\"Kanix City\"},{\"ID\":54,\"Text\":\"KANIX DREAM CITY\"},{\"ID\":59,\"Text\":\"KANIX DREAM CITY -- PHASE II\"},{\"ID\":62,\"Text\":\"KANIX DREAM CITY PHASE I\"},{\"ID\":55,\"Text\":\"Kishor_TEST\"},{\"ID\":63,\"Text\":\"Next Generation Housing\"},{\"ID\":65,\"Text\":\"Nothing Job\"},{\"ID\":56,\"Text\":\"PAVAN_TEST\"},{\"ID\":46,\"Text\":\"PRODUCTION UNITS\"},{\"ID\":1,\"Text\":\"PROJECT-01(TYPE 1)\"},{\"ID\":3,\"Text\":\"PROJECT-02(TYPE 1)\"},{\"ID\":5,\"Text\":\"PROJECT-03(TYPE 1)\"},{\"ID\":6,\"Text\":\"PROJECT-04(TYPE 1)\"},{\"ID\":7,\"Text\":\"PROJECT-05(TYPE 1)\"},{\"ID\":8,\"Text\":\"PROJECT-06(TYPE 1)\"},{\"ID\":2,\"Text\":\"PROJECT-07(TYPE 2)\"},{\"ID\":4,\"Text\":\"PROJECT-08(TYPE 2)\"},{\"ID\":9,\"Text\":\"PROJECT-09(TYPE 3)\"},{\"ID\":10,\"Text\":\"PROJECT-10(TYPE 3)\"},{\"ID\":11,\"Text\":\"PROJECT-11(TYPE 4)\"},{\"ID\":57,\"Text\":\"Reviera Classic\"},{\"ID\":43,\"Text\":\"ROAD PROJECT\"},{\"ID\":41,\"Text\":\"SAMPLE PROJECT 1\"},{\"ID\":42,\"Text\":\"SAMPLE PROJECT 2\"},{\"ID\":52,\"Text\":\"Shailesh Test project#1000\"},{\"ID\":61,\"Text\":\"VISHAL PARADISE\"},{\"ID\":60,\"Text\":\"WTC\"}]"
my full code is like this:
#Override
protected List<CItem> doInBackground(String... params) {
try {
String line="";
String ur = "http://"+ServerDetails.hostServer+"/appservices.svc/Projects?Keyword=" ;
lstItm=new ArrayList<CItem>() ;
// Replace it with your own WCF service path
URL json = new URL(ur);
URLConnection jc = json.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
line = reader.readLine();
Log.d("LINE",line);
JSONArray array=new JSONArray(line);
Itm=new CItem( "-1", "Select Project" );
lstItm.add(Itm);
for(int i=0; i < array.length(); i++) {
JSONObject tmpJson=array.getJSONObject(i);
Itm=new CItem(tmpJson.getString("ID"),tmpJson.getString("Text"));
lstItm.add(Itm);
}
return lstItm ;
}
catch(Exception e)
{
Log.d("ERRROR--->",e.getMessage());
}
return lstItm ;
}
#mubu9082 ..you dont need to remove these backslashes...
as this json string is shown with backslashes in log or by debugger..
just parse it as usual
public void jsonParser()
{
ArrayList<> list=new ArrayList<>(); //declare this as global
String responseString="[{\"ID\":51,\"Text\":\"!! SAMPLE PROJECT !!\"},{\"ID\":58,\"Text\":\"01 Contracting Test Project\"},{\"ID\":64,\"Text\":\"1212\"},{\"ID\":45,\"Text\":\"CHEMICAL FACTORY PROJECT\"}]";
JSONArray array=new JSONArray(responseString);
String id[]=new String[array.length()];
String text[]=new String[array.length()];
for(int i=0;i<array.length();i++)
{
JSONObject tmpJson=array.getJSONObject(i);
id[i]=tmpJson.getString("ID");
text[i]=tmpJson.getString("TEXT");
CItem Itm=new CItem(tmpJson.getString("ID"),tmpJson.getString("Text")); lstItm.add(Itm);
list.add(Itm);
}
}
do this to get response from server
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL ...use
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
HttpEntity entity = httpResponse.getEntity();
String response= EntityUtils.toString(entity);
//pass this response to JSONArray object
//save response and then flush the entity.
entity.consumeContent();
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
pass this response to JSONArray object
public InsuranceDO getInsuranceData1(Context context) {
String urlStr = "http://192.168.2.11:8080/Service/category/sample";
InsuranceDO insuranceDO = new InsuranceDO();
HttpURLConnection urlConnection;
List<InsuranceDO> insList = new ArrayList<InsuranceDO>();
try {
String reqVal = "T=421D84EAC8DEB4878CE48C8A0CB870791EB96FE51C7800A8806032A8CE69A4966D87FFA2E139EE6586C1924F9BD070154CB7E8F92985AC6674B0AD37D9F3FC1ED7B2E4C2D01E5525DCE5E6FCDA26AF890633011894AA2B72604CC8B046E4F9C37DE9A61EECD7000325D3EC673E8609AAD753C52B9BC002C014BC18A35AA8AB3636C237088A08EEED72A7C5F2EDE60155E9111A6F74F082C0E4B45D484C00CA5AD5B3560B8A10D47616E48077EBDE490E&UserCode=172278&DBSource=bali";
URL url = new URL(urlStr);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(reqVal.getBytes());
outputStream.flush();
int code = urlConnection.getResponseCode();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
StringBuilder result = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
/**
* To parse json to list data
*/
JSONArray jsonArray = new JSONArray(result.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
insuranceDO.setAgeing(jsonObject.getString("xxx"));
insuranceDO.setInsuredName(jsonObject.getString("yyyy"));
insuranceDO.setProposalNumber(jsonObject.getString("zzzz"));
insuranceDO.setReason(jsonObject.getString("aaaa"));
insList.add(insuranceDO);
}
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Toast.makeText(context, insList.toString(), Toast.LENGTH_LONG).show();
return insuranceDO;
}
I'm trying to find Java's equivalent to Groovy's:
String content = "http://www.google.com".toURL().getText();
I want to read content from a URL into string. I don't want to pollute my code with buffered streams and loops for such a simple task. I looked into apache's HttpClient but I also don't see a one or two line implementation.
Now that some time has passed since the original answer was accepted, there's a better approach:
String out = new Scanner(new URL("http://www.google.com").openStream(), "UTF-8").useDelimiter("\\A").next();
If you want a slightly fuller implementation, which is not a single line, do this:
public static String readStringFromURL(String requestURL) throws IOException
{
try (Scanner scanner = new Scanner(new URL(requestURL).openStream(),
StandardCharsets.UTF_8.toString()))
{
scanner.useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : "";
}
}
This answer refers to an older version of Java. You may want to look at ccleve's answer.
Here is the traditional way to do this:
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static String getText(String url) throws Exception {
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
return response.toString();
}
public static void main(String[] args) throws Exception {
String content = URLConnectionReader.getText(args[0]);
System.out.println(content);
}
}
As #extraneon has suggested, ioutils allows you to do this in a very eloquent way that's still in the Java spirit:
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
System.out.println( IOUtils.toString( in ) );
} finally {
IOUtils.closeQuietly(in);
}
Or just use Apache Commons IOUtils.toString(URL url), or the variant that also accepts an encoding parameter.
There's an even better way as of Java 9:
URL u = new URL("http://www.example.com/");
try (InputStream in = u.openStream()) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
Like the original groovy example, this assumes that the content is UTF-8 encoded. (If you need something more clever than that, you need to create a URLConnection and use it to figure out the encoding.)
Now that more time has passed, here's a way to do it in Java 8:
URLConnection conn = url.openConnection();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
pageText = reader.lines().collect(Collectors.joining("\n"));
}
Additional example using Guava:
URL xmlData = ...
String data = Resources.toString(xmlData, Charsets.UTF_8);
Java 11+:
URI uri = URI.create("http://www.google.com");
HttpRequest request = HttpRequest.newBuilder(uri).build();
String content = HttpClient.newHttpClient().send(request, BodyHandlers.ofString()).body();
If you have the input stream (see Joe's answer) also consider ioutils.toString( inputstream ).
http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html#toString(java.io.InputStream)
The following works with Java 7/8, secure urls, and shows how to add a cookie to your request as well. Note this is mostly a direct copy of this other great answer on this page, but added the cookie example, and clarification in that it works with secure urls as well ;-)
If you need to connect to a server with an invalid certificate or self signed certificate, this will throw security errors unless you import the certificate. If you need this functionality, you could consider the approach detailed in this answer to this related question on StackOverflow.
Example
String result = getUrlAsString("https://www.google.com");
System.out.println(result);
outputs
<!doctype html><html itemscope="" .... etc
Code
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public static String getUrlAsString(String url)
{
try
{
URL urlObj = new URL(url);
URLConnection con = urlObj.openConnection();
con.setDoOutput(true); // we want the response
con.setRequestProperty("Cookie", "myCookie=test123");
con.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
String newLine = System.getProperty("line.separator");
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine + newLine);
}
in.close();
return response.toString();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
Here's Jeanne's lovely answer, but wrapped in a tidy function for muppets like me:
private static String getUrl(String aUrl) throws MalformedURLException, IOException
{
String urlData = "";
URL urlObj = new URL(aUrl);
URLConnection conn = urlObj.openConnection();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)))
{
urlData = reader.lines().collect(Collectors.joining("\n"));
}
return urlData;
}
URL to String in pure Java
Example call to get payload from http get call
String str = getStringFromUrl("YourUrl");
Implementation
You can use the method described in this answer, on How to read URL to an InputStream and combine it with this answer on How to read InputStream to String.
The outcome will be something like
public String getStringFromUrl(URL url) throws IOException {
return inputStreamToString(urlToInputStream(url,null));
}
public String inputStreamToString(InputStream inputStream) throws IOException {
try(ByteArrayOutputStream result = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString(UTF_8);
}
}
private InputStream urlToInputStream(URL url, Map<String, String> args) {
HttpURLConnection con = null;
InputStream inputStream = null;
try {
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(15000);
con.setReadTimeout(15000);
if (args != null) {
for (Entry<String, String> e : args.entrySet()) {
con.setRequestProperty(e.getKey(), e.getValue());
}
}
con.connect();
int responseCode = con.getResponseCode();
/* By default the connection will follow redirects. The following
* block is only entered if the implementation of HttpURLConnection
* does not perform the redirect. The exact behavior depends to
* the actual implementation (e.g. sun.net).
* !!! Attention: This block allows the connection to
* switch protocols (e.g. HTTP to HTTPS), which is <b>not</b>
* default behavior. See: https://stackoverflow.com/questions/1884230
* for more info!!!
*/
if (responseCode < 400 && responseCode > 299) {
String redirectUrl = con.getHeaderField("Location");
try {
URL newUrl = new URL(redirectUrl);
return urlToInputStream(newUrl, args);
} catch (MalformedURLException e) {
URL newUrl = new URL(url.getProtocol() + "://" + url.getHost() + redirectUrl);
return urlToInputStream(newUrl, args);
}
}
/*!!!!!*/
inputStream = con.getInputStream();
return inputStream;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Pros
It is pure java
It can be easily enhanced by adding different headers as a map (instead of passing a null object, like the example above does), authentication, etc.
Handling of protocol switches is supported