public class GetPlaceSearchTask extends AsyncTask {enter code here
enter code here
private Context context;
private ProgressDialog dialog;
public static final String SEND_RESULT_SEARCH_BROADCAST_FROM_TASK = "send_result_search";
private String API_LOCATION = "https://maps.googleapis.com/maps/api/place/nearbysearch/" +
"json?location=-33.8670522,151.1957362&radius=500&type=restaurant&name=cruise&key=";
public GetPlaceSearchTask(Context context) {
this.context = context;
}
protected void onPreExecute() {
if (context != null) {
dialog = new ProgressDialog(context);
dialog.setTitle("Downloading");
dialog.show();
}
}
@Override
protected String doInBackground(String... params) {
HttpsURLCoection coection = null;
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
try {
URL url = new URL(String.format(API_LOCATION, params[0], params[1]));
coection = (HttpsURLCoection) url.openCoection();
if (coection.getResponseCode() != HttpsURLCoection.HTTP_OK) {
retu null;
}
reader = new BufferedReader(new InputStreamReader(coection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
enter code here
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (coection != null) {
coection.discoect();
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
retu builder.toString();
}
@Override
protected void onPostExecute(String result) {
if (dialog != null) {
dialog.dismiss();
}
Intent intent = new Intent(SEND_RESULT_SEARCH_BROADCAST_FROM_TASK);
intent.putExtra("result_search", result);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
enter code here
public static class GetPlaceSearchTextTask extends AsyncTask<String , Void , String>{
private String API_TEXT = "https://maps.googleapis.com/maps/api/place/textsearch/" +
"json?query=%1$s&location=[%2$s,%3$s]&radius=5000&key=";
public static final String SEND_BROADCAST_RESULT_TEXT_FROM_TASK = "sand_text_result";
private Context context;
private ProgressDialog dialog ;
public GetPlaceSearchTextTask(Context context){
this.context = context;
}
@Override
protected void onPreExecute() {
if (context != null) {
dialog = new ProgressDialog(context);
dialog.setTitle("Downloading");
dialog.show();
}
}
@Override
protected String doInBackground(String... params) {
HttpsURLCoection coection = null;
BufferedReader reader = null;
StringBuilder builder = new StringBuilder() ;
try {
URL url = new URL(API_TEXT + params[0] + params[1] + params[2]) ;
coection = (HttpsURLCoection) url.openCoection();
if (coection.getResponseCode() != HttpsURLCoection.HTTP_OK){
retu null;
}
reader = new BufferedReader(new InputStreamReader(coection.getInputStream())) ;
String line;
while ((line=reader.readLine())!= null){
builder.append(line);
}`enter code here`
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (coection!=null)
coection.discoect();
if (reader!=null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
retu builder.toString();
}
@Override
protected void onPostExecute(String result_text) {
if (dialog!=null){
dialog.dismiss();
}`enter code here`
Intent intent = new Intent(SEND_BROADCAST_RESULT_TEXT_FROM_TASK);
intent.putExtra("text" , result_text);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
}
}enter code here
