I write a program to capture the JSON response from the server which contain some needed information I needed. I discovered that sometime my program will not able to capture the correct JSON string and sometime it's works well with no problem. I try to check my code for capturing the response and have no idea on it. When I check the JSON string from server, it's contain the field I want but my program not able to capture the correct data.
This is my JSON String
"info":{
"reason":"Fine",
"boolean":false,
"post":{
"actions":"",
"actions_In_process":"Checked",
"destination":"C%3ApdfFdoc%20edipdfFdestinationpdfFexample.pdf",
"file_type":"pdf",
},
This is my program for capture the JSON string and the field I need is action_In_process
String Url1 = "http://IP:port/etc/";
HttpURLConnection con = (HttpURLConnection) Url1.openConnection();
con.setRequestMethod("GET");
con.connect();
int responseCode = con.getResponseCode();
if(responseCode == 200)
{
try
{
InputStream is = con.getInputStream();
BufferedReader read = new BufferedReader (new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String data = "" ;
while((data = read.readLine() ) != null )
{
buffer.append(data);
}
String JsonData = buffer.toString();
JSONObject jobj = new JSONObject(JsonData);
JSONObject process_info = jobj.getJSONObject("info");
JSONObject pi = process_info.getJSONObject("post");
String action_run = pi.getString("actions_In_process");
System.out.println("Process:" +action_run);
What I had found out is sometime the Process showing is blank but when I get back the JSON data and I found out the field I need is inside the JSON response. Please share your opinion on this issues
This is the message showing my compiler if I not able to capture the correct JSON string
Process :
If in normal condition
Process : check
BufferedReader's readline() is blocking.
Related
Getting stuck in an infinite-loop while retrieving api result.
Initialy I am getting scan percentage for 2-3 times but after that I am not getting any response.
My java code:
int responseCode=200;
String responseText = "text";
while (!responseText.equalsIgnoreCase("100") && responseCode == 200) {
URL urlForGetRequest = new URL("http://localhost:8090/burp/scanner/status");
String readLine = null;
HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();
conection.setRequestMethod("GET");
responseCode = conection.getResponseCode();
System.out.println("response" + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conection.getInputStream()));
String response = "";
while ((readLine = in.readLine()) != null) {
response += (readLine);
}
in.close();
JSONObject jsonObj = new JSONObject(response);
// print result
responseText = String.valueOf(jsonObj.get("scanPercentage"));
System.out.println(responseText);
TimeUnit.MINUTES.sleep(2);
}
output I got
response200
0
response200
4
response200
14
response200
17
and after this code kept on running without any output
Note: I perform get from its swagger UI,there is 1 error. i.e TypeError: Failed to fetch
There may be a chance the issue is in the response text check the actual text what the sender is sending
or just eliminate the extra space from the response text using following oneline code before processing the response text. It may be work for you if such problem is there..
responseText=responseText.replace(/^\s+|\s+$/g, '');
Put the code in try, catch block and check what exception it's throwing.
You could try to check if it gets stuck in the while loop, by adding an output and see if it keeps writing when it gets stuck.
while ((readLine = in.readLine()) != null) {
response += (readLine);
System.out.print(".");
}
I don't think this is the reason but it is worth a try.
I use java to get youtube video url from a youtube url,it seems like i should decode video url twice to get the original video url,but something is wrong when i access the video url which i get from my code. Im sure i can access any video url which i get when i first run the code after i finish the code, but after that ,i can not access any video url i get no matter the video url is from decode first or twice. I suppose maybe only youtube itself can access the video url i get or the server blocked my IP. So i need you guys to help me to tell me whats wrong and how i could do to get the video url that can play, thx. My code and answer is below:
try {
String ytUrl = "https://www.youtube.com/watch?v=Cj3AV92fJ90";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(ytUrl);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line.replace("\\u0026", "&"));
}
in.close();
html = str.toString();
String val=RegexUtil.find(html,"stream_map\":.*?\"(.*?)\"",1);
if (!StringUtils.isEmpty(val)) {
String url = URLDecoder.decode(val, "UTF-8");
System.out.println("1 decode url: "+url);
url = URLDecoder.decode(url, "UTF-8");;
System.out.println("2 decode url: "+url );
}
} catch (Exception e) {
e.printStackTrace();
}
1 decode url: url=https://r4---sn-i3b7knlk.googlevideo.com/videoplayback?id=o-AE4MBDOnLdC4X0a7wjJ63diBNkBpJ2XiuejUOXrB8onv&dur=421.442&mime=video%2Fmp4&fvip=4&requiressl=yes&ms=au%2Conr&mt=1540798558&ratebypass=yes&itag=22&pl=25&sparams=dur%2Cei%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&source=youtube&mv=m&mn=sn-i3b7knlk%2Csn-npoeene7&lmt=1537570091802082&key=yt6&ei=8LjWW9P1Ns2R8gOGhKO4AQ&c=WEB&expire=1540820305&ip=114.113.240.105&ipbits=0&initcwndbps=362500&mm=31%2C26&itag=22&type=video/mp4; codecs="avc1.64001F, mp4a.40.2"&s=C8C80C74C92B4306498EE1183D683A0E60962F69BD.4F1AE9CE009B83425F389362CFC2FE23A45948D5&quality=hd720&sp=signature
2 decode url: url=https://r4---sn-i3b7knlk.googlevideo.com/videoplayback?id=o-AE4MBDOnLdC4X0a7wjJ63diBNkBpJ2XiuejUOXrB8onv&dur=421.442&mime=video/mp4&fvip=4&requiressl=yes&ms=au,onr&mt=1540798558&ratebypass=yes&itag=22&pl=25&sparams=dur,ei,id,initcwndbps,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,ratebypass,requiressl,source,expire&source=youtube&mv=m&mn=sn-i3b7knlk,sn-npoeene7&lmt=1537570091802082&key=yt6&ei=8LjWW9P1Ns2R8gOGhKO4AQ&c=WEB&expire=1540820305&ip=114.113.240.105&ipbits=0&initcwndbps=362500&mm=31,26&itag=22&type=video/mp4; codecs="avc1.64001F, mp4a.40.2"&s=C8C80C74C92B4306498EE1183D683A0E60962F69BD.4F1AE9CE009B83425F389362CFC2FE23A45948D5&quality=hd720&sp=signature
Currently I'm trying how to extract this information in the jar file to pass server the information required.
When you trigger this url:
http://ipinfo.io/country
But the return will be in a 2 variable , so my problem is how to extract since it's not a JSON.
try {
URL obj = new URL("http://ipinfo.io/country");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
con.setDoInput(true);
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String joinString = "";
String decodedString;
while ((decodedString = in.readLine()) != null) {
joinString = joinString + decodedString;
}
in.close();
//-- Logging (joinString) & responseCode
this.setCountry(new JSONObject(joinString).getString(""));
} catch (IOException ex) {
Logger.getLogger(RegUserRequest.class.getName()).log(Level.SEVERE, null, ex);
}
the http://ipinfo.io/country get request returns a country code as text output.
So why not simply doing :
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String countryCode = in.readLine();
If it provides directly the data and that you have a single data to retrieve, why do you want to use JSON ?
You can print it with:
System.out.println(joinString);
If you need The GR string is in your joinString as plain text.
They provide a JSON API that returns JSON and the country code.
But please also consider what they tell you about using their service / API:
Free usage of our API is limited to 1,000 API requests per day. If you
exceed 1,000 requests in a 24 hour period we'll return a 429 HTTP
status code to you. If you need to make more requests or custom data,
see our paid plans, which all have soft limits.
(Taken from https://ipinfo.io/developers/getting-started)
Will try to explain my question here.
I have a program that is suppose to parse through an incoming JSON-file that I receive from a web-crawler.
public static void Scan(Article article) throws Exception
{
//When running program, creates a error text-file inside java Project folder
File file = new File("errorlogg.txt");
FileWriter fileWriter = new FileWriter(file, true);
// if file doesn't exists, then create it
if (!file.exists())
{
file.createNewFile();
}
//Setting up an URL HttpURLConnection given DOI
URL urlDoi = new URL (article.GetElectronicEdition());
//Used for debugging
System.out.println("Initial DOI: " + urlDoi);
//Transform from URL to String
String doiCheck = urlDoi.toString();
//Redirect from IEEE Xplore toe IEEE Computer Society
if(doiCheck.startsWith("http://dx."))
{
doiCheck = doiCheck.replace("http://dx.doi.org/", "http://doi.ieeecomputersociety.org/");
urlDoi = new URL(doiCheck);
}
HttpURLConnection connDoi = (HttpURLConnection) urlDoi.openConnection();
// Make the logic below easier to detect redirections
connDoi.setInstanceFollowRedirects(false);
String doi = "{\"url\":\"" + connDoi.getHeaderField("Location") + "\",\"sessionid\":\"abc123\"}";
//Setting up an URL to translation-server
URL url = new URL("http://127.0.0.1:1969/web");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(doi);
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null )
{
//Used to see of we get something from stream
System.out.println(line);
//Incoming is JSONArray, so create new array and parse fill it with incoming information
JSONArray jsonArr = new JSONArray(line);
JSONObject obj = jsonArr.getJSONObject(0);
//Check if names from DBLP is the same as translators get
//AuthorName, from field creators
JSONArray authorNames = obj.getJSONArray("creators");
ArrayList<Author> TranslationAuthors = new ArrayList<Author>();
Here is the bit of the code that I'm talking about. As you can see I wanna run this code when I get some information from the bufferreader.
My problem is that my program doesn't seem to skip when I don't get a valid JSON. Instead it runs to this line of code:
JSONArray authorNames = obj.getJSONArray("creators")
And then is forced to exit since it can't get the field "creators" since there is none.
How can I do to make sure that my program don't encounter this problem? How can I easy put it in the error-logg file that I create that I could't collect any information.
I think you are working with a org.json.JSONObject? If that's so, there is a has method, which can be used to avoid the JSONException in case the key does not exist.
JSONArray authorNames = null;
if (obj.has("creators")) {
authorNames = obj.getJSONArray("creators");
}
conn = (HttpURLConnection) connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.connect();
int code = conn.getResponseCode();
I have successfully established a connection. I am trying to pass the information over the internet.When the url is opened via browser I am getting response as
{"status":"0","responseCode":"1001","response":"Wrong Settings."}
For correct status is returned as 1.
Is there any method where I can get the status only.I have been trying the following methods but every time I am getting code (below is code snippet) as -1 irrespect of status code when I am verifying manually via browser
This is a JSON text. You will need to use a JSON library.
int code = conn.getResponseCode();
this method returns http status code, for http status codes see
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
while the response code you want to retrieve is actually the response string returned by the server.
To read this use:
try {
InputStream in = new BufferedInputStream(conn.getInputStream());
readStream(in);//method to read characters from stream.
finally {
urlConnection.disconnect();
}
}
You can add below code for get Response string from your connection.
OutputStream connectionOutput = null;
connectionOutput=connection.getOutputStream();
connectionOutput.write(requestJson.toString().getBytes());
connectionOutput.flush();
connectionOutput.close();
inputStream = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream dataCache = new ByteArrayOutputStream();
// Fully read data
byte[] buff = new byte[1024];
int len;
while ((len = inputStream.read(buff)) >= 0) {
dataCache.write(buff, 0, len);
}
// Close streams
dataCache.close();
Now get Response string of json like below.
String jsonString = new String(dataCache.toByteArray()).trim();
JSONObject mJsonobject=new JSONObject(jsonString);
You can now parse your key from this mJsonobject Object.