how to get String by spliting url in android? - android

I have a google play store url "https://play.google.com/store/apps/details?id=com.apusapps.launcher&hl=en" from this url I have to store"com.example.launcher" in a seperate variable and compare from package name "com.example.launc" if compare I have to send a json request .how can I do that

Try this:
String s = "https://play.google.com/store/apps/details?id=com.apusapps.launcher&hl=en";
String s2 = s.substring(s.indexOf("=")+1, s.indexOf("&"));
Toast.makeText(this,"Milad: "+s2,Toast.LENGTH_LONG).show();

You can do like this:
String urlString = url.toString();
String finalString = urlString.substring(urlString.indexOf("=")+1,urlString.lastIndexOf("&"));
if(finalString.equals(getApplicationContext().getPackageName())){
//Your code
}

Related

Upload Image Firebase

why my method and taskSnapshot doesn't work ?
and here's my POJO
Add other 4 parameters using coma ,
As declared in your UserInformation you need to put all parameter to create the object.
Try something like that :
String judul = "";
String isi = "";
String penulis = "";
String kat = "";
String url = taskSnapshot.getDownloadUrl().toString;
UserInformation userInformation = UserInformation(judul , isi, penulis, kat, url)

Is there any way to convert JSON String to query String in android?

I am stuck at converting a Json string into query string.
Actually, I want to create query string and from that query string I'll generate a SHA hash and set it in header to send to the server.
Please help!
Is your JSON String currently stored in a JSONObject? If not, this would be the best place to start. Something like this:
JSONObject json = new JSONObject(returnedString);
String firstValue= json.get(firstKey);
String secondValue = json.get(secondKey);
//And then construct your query string using the obtained values
Edit
Generic Method as suggested below...
Something like this:
public String getQueryString(String unparsedString){
StringBuilder sb = new StringBuilder();
JSONObject json = new JSONObject(unparsedString);
Iterator<String> keys = json.keys();
sb.append("?"); //start of query args
while (keys.hasNext()) {
String key = keys.next();
sb.append(key);
sb.append("=");
sb.append(json.get(key);
sb.append("&"); //To allow for another argument.
}
return sb.toString();
If you have more questions regarding JSONObject parsing in Android these docs are very helpful
On Android :
Uri uri=Uri.parse(url_string);
uri.getQueryParameter("para1");
On Android, the Apache libraries provide a Query parser:
http://developer.android.com/reference/org/apache/http/client/utils/URLEncodedUtils.html and http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html

Parse JSON to webview Android

I retrieve data from mysql by php and save it as String
read= new BufferedReader(new InputStreamReader(s,"iso-8859-1"),8);
String line=null;
while ((line=read.readLine()) !=null)
{
text+=line+"\n";
}
output is
[{"DATE_OF_TEXT":"2015-05-06","total_rate_FORuser":"9.90000"},
{"DATE_OF_TEXT":"2015-05-30","total_rate_FORuser":"5.10000"}]
How parse JSON to my Javascript file in webview ?
To pass in the string after the page is loaded, you could use
String jsText = "javascript:addData('" + text + "');");
webView.loadUrl(jsText);
EDIT : In your case, modify the url with the string:
String urlWithData = yourUrl + "?data=" + text;
webView.loadUrl(urlWithData);
Then in the javascript, first get the text using window.location.search
var jsontext = window.location.search.substring(1).split('=')[1];
And then, convert the JSON text into a JavaScript object, using JSON.parse
var obj = JSON.parse(jsontext);

How to split the string a android

I am working on android application. I am getting the String value from webservice extension
i want to split 4 from the string by using index .pls tell me how can do this
String version = "1.4.2";
Try this..
String version = "1.4.2";
Log.v("value ",""+version.split("\\.")[1]);
For more information Refer Link1,Link2
can you try this :
String[] items = "1.4.2".split("\\.");
String version = items[1].toString();
have all the sub-part of your string using
String[] strs = version.split("\.");
now if you want 4 from (1.4.2), do following:
String mMiddle = strs[1]; // it will give 4 as a string in mMiddle
if you want every sub-part:
String mStart = strs[0]; //returns 1
String mMiddle = strs[1]; //returns 4
String mLast = strs[2]; //returns 2
Try this code:
String version = "1.4.2";
String arr[]=version.split("\\.");
String value=arr[1].toString();
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
thank you.

Android: Divide one String into 2 String values from responseBody

With this function I got a String from the server as a response:
String responseBody = EntityUtils.toString(response.getEntity());
the String value I retrieve looks more or less like this:
"http://url.com 765889"
I want to divide the URL and the numbers into 2 String values, it should be:
String 1="http://url.com"
String 2="765889"
How am I able to perform that?
Use the split() function:
String[] parts = responseBody.split(" ");
Maybe like this
String 1 = responseBody.substring(0,responseBody.indexOf(" ")-1);
String 2 = responseBody.substring(responseBody.indexOf(" ").responseBody.length()-1);
Simples do this
StringTokenizer t = new StringTokenizer("http://url.com 765889"," ");
Log.d("first", t.nextToken());
Log.d("second", t.nextToken());

Categories

Resources