How do I get dynamic query text from a URL? - java

How can I get the value "2" of "entityId=2" from this example Url: https://test.com/form/form.htm?Index=0&entityId=2&wid=74&_wid=74 using Java and assuming this value is not static?
I am using:
URL url = new URL("https://test.com/form/form.htm?Index=0&entityId=2&wid=74&_wid=74");
String quesries = url.getQuery();
int i = quesries.length();`enter code here`
System.out.println(i);
Which gave me length of 33.

You just have to use the substring method in Java.
String entityId = urlString.substring(urlString.indexOf("entityId=")+9,urlString.indexOf("&wid"))

Related

How do i get parameter value from url as string

I have one request which response is displayed as url below. I need to extract user_token value from url and pass to subsequent request.
response url: http://example.com?user_token=0c1c59bc-3aaa-40f1-b978-7172de09a27f&m_id=9999&code=200&is_register=false&M=SUCCESS
i want to extract user_token from it and want to pass it to subsequent request, need solution in java code not java script.
You can do this:
String getTokenId(String url){
String[] splitUrl = url.split("user_token=");
String tokenId = "";
if(splitUrl.length >1){
for(int i =0; i < splitUrl[1].length(); i++){
if(splitUrl[1].charAt(i) == '&'){
tokenId = splitUrl[1].substring(0,i);
break;
}
}
}
return tokenId;
}
But if you know the exact length of the token, it could be a search.
Maybe what you need is request.getParameter("user_token"). You can do this in servlet for example

Getting String value using JsonPath Java

Im using JsonPath to Assert on some of the values returned from an api response but having a small issue with asserting the response.
Heres the response i get:
[{"Id":75969,StartDate":"2016-07-01","EndDate":"2021-04-30","duration":5640}]
Lets say i want to Assert on the Start date.
The method I created looks like this:
public void checkStartDate(String expectedStartDate) {
String responseBody = response.getBody().asString();
JsonPath values = new JsonPath(responseBody);
Assert.assertEquals(expectedStartDate,values.getString("startDate"));
}
The expectedStartDate that i pass in to the method is 2016-07-01 and the date i get back from the JsonPath object is [2016-07-01] which causes the assertion to fail.
Does anyone know what i can do to with JsonPath in order to remove the square braces from the value that im extracting from the response string?
This might help you. Try the below code :
public void checkStartDate(String expectedStartDate) {
String responseBody = response.getBody().asString();
String startDate = JsonPath.read(responseBody, "$.[0].StartDate");
Assert.assertEquals(expectedStartDate, startDate);
}
When you read with "getList", you will get the exact value.
JsonPath values = new JsonPath(responseBody);
List<String> valuesToRead = values.getList("StartDate");
for (String value : valuesToRead) {
System.out.println(value);
}
I think the problem is the reponse is an array of objects, rather than just a single object? So you want to check against jp.getString("[0].StartDate")
Working example:
JsonPath jp = JsonPath.from("[{\"Id\":75969,\"StartDate\":\"2016-07-01\",\"EndDate\":\"2021-04-30\",\"duration\":5640}]");
System.out.println("2016-07-01".equals(jp.getString("[0].StartDate")));
Prints: true

How to replace a string in jsp

I have a string url like http://google.com. I need to remove 'http://' from the URL. I have tried the method .replace("http://",""), but it is not working.
Web web = org.getWeb();
webUrl = web.getUrl();
out.println("webUrl :"+webUrl ); // here it prints:: http://google.com
webUrl.replace("http://","");
out.println("webUrl :"+webUrl ); // here also it prints:: http://google.com
Try:
webUrl = webUrl.replace("http://","");
As replace returns the replaced string
You need to do like following.String is immutable class.replace will return you new String object.
webUrl = webUrl.replace("http://","");
Refer String is immutable. What exactly is the meaning?

How to capture the specific text inside the URL?

I have capture the current URL on the page.
using :
String url = driver.getCurrentUrl();
Now I want a specific text inside this string. Let say
String url = http://www.youtube.com/watch?v=R5-gtsdenpE
and I want
Capture and store the current page URL in String URL. (Done)
Capture the text in the URL after "v" and store it in String emb. (??)
I am using JAVA to write my scripts on Ubuntu.
Use:
String fullURL = http://www.youtube.com/watch?v=R5-gtsdenpE;
String emb = fullURL.split("\\?v=")[1];
This is what you want I guess-
String string = "http://www.youtube.com/watch?v=R5-gtsdenpE";
URL url = new URL(string);
System.out.println(url.getQuery());
Handle the exception appropriately.
In case you don't want to use URL class, just search for the first index of ? and then use substring() to get the string after that.
System.out.println(string.substring(string.indexOf("?")+1));
Url url = new Url(driver.getCurrentUrl());
Map<String, String[]> params = parameterMapFromString(url.getQuery());
String v = params.get("v")[0];
If you are requirement is static and you are sure that you have to get the value after "v" than you can try this also
String emb = url.substring(url.indexOf("v"), url.length()).trim();

How to return PHP array and receive it with Java

I have this on my webservice:
function listar($username)
{
$result = mysql_query("SELECT Name FROM APKs WHERE Tipo=0");
//$registo = mysql_fetch_array($result);
$numero = 0;
while($registo = mysql_fetch_array($result))
{
$regs[$numero] = $registo['Name'];
$numero++;
}
return $regs;
//return mysql_fetch_array($result);
}
in Java, after the SOAP call (not relevant now) I read it this way:
Object response = envelope.getResponse();
String x = response.toString();
I need to access which one of those fields (selected from the database) so I thought, why not split the array into strings?
I tried two methods:
String[] arr=x.split(" ");
System.out.println("Array :"+arr.length);
for(int i=0;i<arr.length;i++)
{
..
}
StringTokenizer stArr=new StringTokenizer(x," ");
while(stArr.hasMoreTokens())
{
...
}
But none of them worked, whick make me believe I'm returning badly the array in first place.
Any help?
UPDATE:
So I'm using again xsd:string;
Now I have on my webservice return json_encode($regs);
To convert the object from the response I'm using a specific google api
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Object response = envelope.getResponse();
Gson gson = new Gson();
String jstring = gson.toJson(response);
But I'm with difficulty parsing the "jstring" because it's format: "[\"SOMETHING\",\"SOMETHING\",\"SOMETHING\",\"SOMETHING\",.....]". I have not any identifier to get those values.
How can I extract those dynamic values and assign them to String[]?
USE:
json_encode($array); in PHP
and
JSONObject in Java to read.
See this example: http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
UPDATE
Change the line:
$regs[$numero] = $registo['Name'];
to:
$regs[] = array('name' => $registo["name"]);
If you need to get the ID, you also can do:
$regs[] = array('name' => $registo["name"], 'id' => $registo["id"]);
FORGET:
The mysql_fetch_array returns a real array not a string. It's not needed to split a string.
Be sure that your PHP Web Service is returning a xsd:array
Generate a new proxy using some generator like http://www.soapui.org. Just use the proxy. Nothing else.
Try to use JSON and you can esy build easy object and read it.

Categories

Resources