I have following problem. I have android app with some data and I want to post json data to web api. Api notice when you load url with json like this.
http://example.eu/xx/yyyy/{"data1":"TEXT","data2":"TEXT","data3:"TEXT","data4":"TEXT"}
but when I do it like this ....
URL url = new URL("http://example.eu/xx/yyyy/"+json);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
It work but in api i see this
"{\"id\":\"14D20E65-5701-4519-AD87-1FBBD80706CF\",\"stav\":
\"SENT\"}"
There are quotation marks with backslash. And I don't know how to remove them. (I think the problem is when i load string url to URL class)
Related
URL url = new URL("https://www.cs.tut.fi/~jkorpela/html/iframe-pdf.html");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
When calling on getInputStream, i turn all the bytes into a string. But why am i not seeing any sign of the data in the iframe?
My goal is to download the PDF.
If you request a URL, you will only get the contents of that file. An iframe is normally effectively a seperate page, so you would need to request that seperately. A browser will normally do all this transparently.
I would recommend using a library such as JSoup which contains lots of methods for parsing HTML, which you will need to get the URL of the iframe (and the URL of the PDF).
If I give any URL, I want to know whether the URL is video URL or audio URL.
Is there any API to find ?
Thanks!
This is not a function of the Java language. You might be able to find some third party library that does what you want, but you have not provided enough information to determine what it is you really want in any reasonable set of circumstances. The following might do what you want, though.
You could just get the ContentType from the http request using something like this:
URL url = new URL(myUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
String contentType = connection.getContentType();
A list of common video ContentTypes that you could detect for are listed on Wikipedia. You'll see video/avi and video/mpeg listed, for example.
I am new to REST API and I want to make a REST API call which returns a JSON object
http://smlookup.dev/sec/products?search={"ABC.CP":"123A5"} - Runs fine in a browser and gives a JSON object
how do i get '?search={"ABC.CP":"12345"}' this expression to work as it filter the records
Code i am using is
String serverUrl="http://smlookup.dev/sec/products?search=";
String search=URLEncoder.encode("={\"ABC.CP\":\"12345\"}");
URL url = new URL(serverUrl+search);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("GET");
OutputStream out = httpCon.getOutputStream();
//FAILS GIVING 405 STATUS CODE
int responseCode = httpCon.getResponseCode();
All help or suggestions are helpful
Thanks!
Not sure if its normal but you dont send any data in your POST.
Furthermore you should urlencode your url, the inverted comma are not accepted like that.
URLEncoder.encode("={\"Xref.CP\":\"XAW3123A5\"}");
I have a URL which I pass in that looks like this
http://somecompany.com/restws/ebi/SVI/4048/?Name=Tra&Brand=Software: WebSphere - Open App Servers
It does not like the 2nd parameter (Brand). From a browser this querystring above works fine but as soon as I execute in Java, it fails. When I change the webservice to accept a single parameter, then this URL works fine
http://somecompany.com/restws/ebi/SVI/4048/?Name=Tra
It seems java is having issues with the 2nd parameter. I have tried escape characters and everything else I can think of but nothing seems to work. Please Help!
String uri = "somecompany.com/restws/ebi/SVI/4048/?Name="
+ name+ "&Brand=Software: WebSphere - Open App Servers";
URL url;
try {
url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
}
...
Try url encoding your parameters.
Something like this:
String uri = "somecompany.com/restws/ebi/SVI/4048/?Name=" +name+ "&Brand=";
uri = URLEncoder.encode("Software: WebSphere - Open App Servers", "utf-8");
I'd perhaps use a library that can handle HTTP parameters properly and provide suitable encoding etc. See HttpComponents and the tutorial for more info.
I am using a java servlet to make a facebook POST of a link to a wall.
What I've gotten so far is that the facebook api's breakdown of the name/value pairs are such that the keys are always strings and the values are usually strings.
So for a POST that just posted a message the body of the POST would be:
message=hello
So the POST data would be of the same format as a GET request with name/value pairs. And the values would be URL encoded.
However, I am having trouble with those values that are arrays or objects, like the "application" field of the feed post record. How is this encoded? How are arrays encoded?
Andy
You would be doing a "POST" not a "GET". So the params would be not URL encoded. You are using Java you said? Your "POST" should look something like this:
URL url = new URL(https://graph.facebook.com/<username>/feed);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStreamWriter writer = new OutputStreamWriter(
connection.getOutputStream());
writer.write("access_token=" + access_token + "&message=hello");
writer.close();
writer.close();