// this method is actually fetching the json string
Private Zero getJSON()The last String urlWebService) {{
/ *
Fetching string as json is a network operation
* And we cannot do network operation in the main thread
* So we need an AsyncTask
* Here are the constraints defined
* Null -> we are not passing anything
* Nil -> nothing even on progress updates
* String -> After completion it should return a string and it will be the same string
* * /
class GetJSON Is spread AsyncTask<Zero, Zero, String> {{
// This method will be called before execution
// You can display a progress bar or something
// so that the user understands that he should wait
// As network operation may take some time
@Override
Protected Zero onPreExecute()) {{
Super.onPreExecute());
}
// This method will be called after execution
// So here we are displaying a toast with jog string
@ Override
Protected Zero onPostExecute()String s) {{
Super.onPostExecute()s);
Toast.Makeup()getApplicationContext()), s, Toast.LENGTH_SHORT).Display());
}
// In this method we are fetching the jugon string
@Override
Protected String doInBackground()Zero... voids) {{
try {{
// Creating a URL
URL url = = New URL()urlWebService);
// Opening URL using HttpURLConnection
HttpURLConnection With = = ()HttpURLConnection) url.Openness());
// StringBuilder to read string from object service
Stringburls Sb = = New Stringburls());
// We will use buffered reader to read string from service
Bufferreader Buffer = = New Bufferreader()New InputStreamReader()With.getInputStream())));
// a simple string to read values from each row
String Jasan;
// we do not get reading until we read
whereas ()()Jasan = = Buffer.Line to read())) ! = Invalid) {{
// add it to string builder
Sb.to attach()Jasan + _ ”);
}
// return the string read at the end
The return Sb.Wire()).Trim());
} Hold ()exception I) {{
The return Invalid;
}
}
}
// Creating an asynctask object and executing it
GetJSON getJSON = = New GetJSON());
getJSON.Executed());
}
Source link