I have a url which have a response in Json
lon=-0.1275&pg=0
I parsed the data from it and displayed in ListView, after displaying the 20 fields the above url should be changed to
lon=-0.1275&pg=1
ie the "pg" must change from 0 to as many pages. How to do that?
please provide me some help
Thanks in advance
You can have a class level variable
int page = 0;
and when you make your url just append it like this:
url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg="+page;
and every time increment it with your pagination like this:
page+=10;
do the request again.
You may use string's replace method to modify url string:
String url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg=0";
url = url.replace("&pg=0", "&pg=1");
I think this way, NOT TESTED.
url = "http://dentonsweb.com/app/html/android/get.php?what=Restaurants&lat=51.507222&lon=-0.1275&pg=";
int i=0;
do{
tempURL = url+i;
// get tempURL response
if(response==null) break;
else
{
// parse response
i++;
}
}while(response!=null)
Related
I am developing an app in which I got response like "100.0" from server which I have to split to "100" and save in variable.How can I do that kindly tell me .
You can use this code to parse your JSON response. Store your wallet balance in a String, them trim the value as per your request
JSONObject object = new JSONObject(JsonResponseText);
String walletBal = object.getString("wallet balance");
String trimWalletBal = walletBal.subString(0,walletBal.indexOf('.'));
You can do it this way:
Integer value = jsonObject.getDouble("parameter").intValue();
Assuming that your problem is just to split the "Integer" and "Fraction" part of a "string" value you are getting from the server -
int decimalIdx = stringVal.indexOf('.');
if (decIdx != -1) {
return stringVal.substring(0, decIdx);
}
return stringVal;
If I have a url: http://www.test.com/segment1/segment2/name:tom/segment4/
What is the best way to set a variable equal to "tom"? I figure there's a method that I'm unaware of. For now I'm just parsing but if a method is optimized to do this I'd rather use that.
Thanks
If the url will always be formatted the same parsing is probably fine.
//Parse name from url
public String getName(String url){
String[] spliturl = url.split(":");
String[] arrayofnames = spliturl[1].split("/"); //Segment after the semicolon
return arrayofnames[0]; //But before the /
}
How are you doing it currently?
i am trying to add a two strings to the web service its working fine when i send through browser and it returns "inserted successfully"
but when i run my application its displaying an error as E/Responce(17407): Illegal character in query at index 74: http://purple2.com/beerbro/addgroupon.php?userid=27&data=100001190851696,1|100001640732983,1|100002430763518,1|100002332633534,1|100000549596039,1|1375802933,1|1587585991,1|1569563632,1|100000132426450,1|100004296815391,1|1519195978,1
,
i think there is a problem with "|" this but i am not able to find the solution
here is the code
for(int i=0; i
data=data+friendid[i]+","+status[i]+"|";
data.replaceAll("","%20");
if(i==0){
}
else if(i==10)
{
String res=UrltoValue.getValuefromUrl(DataUrls.addfriendsurl+"?userid="+usid+"&data="+data.substring(0,data.length()-1));
Log.e("res",DataUrls.addfriendsurl+"?userid="+usid+"&data="+data.substring(0,data.length()-1));
Log.e("Responce",res);
data="";
}
if '|' is the problem then use URLEncoder and encode the String
String data = URLEncoder.encode("100001190851696,1|100001640732983,1|100002430763518,1|100002332633534,1|100000549596039,1|1375802933,1|1587585991,1|1569563632,1|100000132426450,1|100004296815391,1|1519195978");
String mainURL = "http://purple2.com/beerbro/addgroupon.php?userid=27&data="+data;
Hope this helps you.
I have one String and into this string I have a url between two characters # such as "Hello world #http://thisurl# my name is Pippo" I want to take the url (http://thisurl) between two #.
How can I do ? Thanks
String data[] = str.split("#"); //spilliting string and taking into array
ArrayList<String> urlList = new ArrayList<String>();
for (int i = 0; i < data.length; i++) {
if(data[i].contains("http://"))
urlList.add(data[i]); //if string contains "http://" it means it is url save int list.
}
now you can get all uls from urlList.get(i) method.
this urlList will give you all the urls available in the string. I dint applied any null or other check. Apply it and try. If want something else try modifying content and checks.
Try String.split(). You really should be trying to google these things first.
here is an example - http://www.java-examples.com/java-string-split-example
The split method divides a string into several strings and store them into an array using a delimiter which can be defined by you.
the second element in the resulting array will be your URL
I'm trying to make a small app (an image gallery from images from the web, were the url's I get from the JSON file that I received). The context of JSON looks like that:
{"images":{
"yXVak":{
"image_hash":"yXVak",
"imgur_page":"http:\/\/imgur.com\/yXVak",
"original_image":"http:\/\/imgur.com\/yXVak.gif",
"large_thumbnail":"http:\/\/imgur.com\/yXVakl.gif",
"small_thumbnail":"http:\/\/imgur.com\/yXVaks.gif",
"message":"I didn't know they made you see THAT well.",
"source":" ",
"date_popular":"2011-07-18 18:45:05"},
.....
I have about 30 more objects that looks like "yXVak".
Now, the problem is, when I'm trying to parse the text, the program can't find the object "yXVak", the exception looks like that: org.json.JSONException: JSONObject["yXVak"] not found.
I parse the JSON file like that:
jObject = new JSONObject(jString);
JSONObject jImages = jObject.getJSONObject("images");
getImages(jImages);
getImages function looks like that:
JSONObject jHash = jImages.getJSONObject("yXVak") ;
String hash = jHash.getString("yXVak");
String page = jHash.getString("http:\\/\\/imgur.com\\/yXVak");
Image[] images = new Image[3];
images[0] = new Image(jHash.getString("original_image"), jHash.getString("http:\\/\\/imgur.com\\/yXVak.gif"));
images[1] = new Image(jHash.getString("large_thumbnail"), jHash.getString("http:\\/\\/imgur.com\\/yXVakl.gif"));
images[2] = new Image(jHash.getString("small_thumbnail"), jHash.getString("http:\\/\\/imgur.com\\/yXVaks.gif"));
String message = jHash.getString("I didn't know they made you see THAT well.");
String source = jHash.getString(" ");
String date = jHash.getString("2011-07-18 18:45:05");
listOfImages.add(new ImageHash(hash, page, images, message, source, date));
...
By debugging I found that the jString object looks right (the whole string that in the file), but the jImages object missing two first objects ("yXVak", and the second one that I didn't show here "6k9yE").
Can someone help me with that please, what did I do wrong?
I thing you should change this lines in your getImages function:-
JSONObject jHash = jImages.getJSONObject("yXVak");
//Changes in this lines.
String hash = jHash.getString("image_hash");
String page = jHash.getString("imgur_page");
// Rest of your code is same.
Please try this out.I think that it will solve your problem,