use of com.se491.chef_ly.http.HttpConnection in project chefly_android by chef-ly.
the class GetRecipesFromServer method loadInBackground.
@Override
public RecipeList loadInBackground() {
String response;
Log.d(TAG, requestPackage.getMethod() + " " + requestPackage.getEndpoint());
try {
HttpConnection http = new HttpConnection();
response = http.downloadFromFeed(requestPackage);
} catch (IOException e) {
e.printStackTrace();
return new RecipeList();
}
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
Type type = new TypeToken<RecipeList>() {
}.getType();
Log.d("AsyncTaskLoader", "Response " + getId() + " -> " + response);
try {
return gson.fromJson(response, type);
} catch (JsonSyntaxException e) {
Log.d(TAG, "Error - RecipeListObject Required: " + e.getMessage());
return new RecipeList();
}
}
use of com.se491.chef_ly.http.HttpConnection in project chefly_android by chef-ly.
the class RecipeDetailActivity method onStart.
/*
private void textViewClicked() {
ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.my_switcher);
View currentView = switcher.getCurrentView();
//switcher.showNext(); //or switcher.showPrevious();
TextView viewText = (TextView) switcher.findViewById(R.id.directionView);
EditText editText = (EditText) findViewById(R.id.hidden_edit_view);
if (currentView instanceof EditText) {
//Save steps and prepare next view (Text view)
directionsForCooking = editText.getText().toString().split("\n");
StringBuilder sb = new StringBuilder();
int count = 1;
for (String s : directionsForCooking) {
sb.append(count);
sb.append(": ");
sb.append(s);
sb.append("\n");
directionsForCooking[count - 1] = s;
count++;
}
steps = sb.toString();
viewText.setText(steps);
}
else if (currentView instanceof TextView) {
// Prepare next view (Edit view)
String tmpSteps = implode("\n", directionsForCooking);
editText.setText(tmpSteps);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
switcher.showNext();
}
private static String implode(String separator, String... data) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length - 1; i++) {
//data.length - 1 => to not add separator at the end
if (!data[i].matches(" *")) {//empty string are ""; " "; " "; and so on
sb.append(data[i]);
sb.append(separator);
}
}
sb.append(data[data.length - 1].trim());
return sb.toString();
}
*/
@Override
protected void onStart() {
super.onStart();
Intent intent = getIntent();
final String recipeID = intent.getStringExtra("recipe");
RecipeInformation result = intent.getParcelableExtra("recipeDetail");
if (result != null) {
recipeDetail = result;
setRecipeInfo();
} else if (//returns true if internet available
NetworkHelper.hasNetworkAccess(RecipeDetailActivity.this)) {
//register to listen the data
RequestMethod requestPackage = new RequestMethod();
requestPackage.setEndPoint(urlString + recipeID);
// or requestPackage.setMethod("POST");
requestPackage.setMethod("GET");
new AsyncTask<RequestMethod, Integer, Long>() {
String resp = "";
@Override
protected Long doInBackground(RequestMethod... params) {
for (RequestMethod r : params) {
try {
HttpConnection http = new HttpConnection();
resp = http.downloadFromFeed(r);
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
Type type;
type = new TypeToken<RecipeInformation>() {
}.getType();
recipeDetail = gson.fromJson(resp, type);
Log.d(TAG, recipeDetail.toString());
} catch (Exception e) {
//e.printStackTrace();
return -1L;
}
Log.d(TAG, resp);
}
return 1L;
}
@Override
protected void onPostExecute(Long aLong) {
super.onPostExecute(aLong);
setRecipeInfo();
}
}.execute(requestPackage);
} else {
//Toast.makeText(RecipeListActivity.this,"No Internet Connection",Toast.LENGTH_LONG).show();
Log.d(TAG, "No Internet Connection");
}
}
use of com.se491.chef_ly.http.HttpConnection in project chefly_android by chef-ly.
the class MyService method onHandleIntent.
//called automaticly when server starts, receives data as an argument
@Override
protected void onHandleIntent(Intent intent) {
RequestMethod requestPackage = intent.getParcelableExtra(REQUEST_PACKAGE);
String sender = intent.getStringExtra("Tag");
Log.d(TAG, "Sender -> " + sender);
Log.d(TAG, "Request Package Endpoint-> " + requestPackage.getEndpoint());
String response;
try {
HttpConnection http = new HttpConnection();
response = http.downloadFromFeed(requestPackage);
} catch (IOException e) {
//e.printStackTrace();
return;
}
Log.d(TAG, "Response -> " + response);
GsonBuilder builder = new GsonBuilder();
//builder.registerTypeAdapter(Uri.class, new JsonDeserializer<Uri>() {
// @Override
// public Uri deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
// return Uri.parse(json.toString());
// }
//});
Gson gson = builder.create();
RecipeList dataItems;
Type type = new TypeToken<RecipeList>() {
}.getType();
dataItems = gson.fromJson(response, type);
Intent messageIntent = new Intent(MY_SERVICE_MESSAGE);
messageIntent.putExtra(MY_SERVICE_RESPONSE_STRING, response);
// messageIntent.putExtra(MY_SERVICE_PAYLOAD, "Service all done!"); //pass data back, set key value and message
if (dataItems != null) {
//pass back the data
messageIntent.putExtra(MY_SERVICE_PAYLOAD, dataItems);
} else {
// unidentified sender - send empty parcel back
messageIntent.putExtra(MY_SERVICE_PAYLOAD, new Parcelable() {
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
});
//pass back the data
}
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getApplicationContext());
manager.sendBroadcast(messageIntent);
}
use of com.se491.chef_ly.http.HttpConnection in project chefly_android by chef-ly.
the class RecipeListActivity method onDestroy.
@Override
protected void onDestroy() {
super.onDestroy();
// Update favorites list for user on server
if (favListAdd.size() > 0 || favListRemove.size() > 0) {
new AsyncTask<String, Integer, Integer>() {
@Override
protected Integer doInBackground(String... params) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
Type type = new TypeToken<ArrayList<Integer>>() {
}.getType();
String bodyAdd = gson.toJson(favListAdd, type);
String bodyRemove = gson.toJson(favListRemove, type);
String token = CredentialsManager.getCredentials(getApplicationContext()).getAccessToken();
if (token == null) {
token = "test";
}
Log.d(TAG, bodyAdd + " " + bodyRemove);
RequestMethod rm = new RequestMethod();
rm.setEndPoint(urlFavsString);
rm.setMethod("POST");
rm.setHeader("Authorization", "Bearer " + token);
rm.setParam("add", bodyAdd);
rm.setParam("remove", bodyRemove);
try {
HttpConnection http = new HttpConnection();
http.downloadFromFeed(rm);
} catch (IOException e) {
return 0;
}
return 200;
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
}
}.execute();
}
if (BuildConfig.DEBUG) {
RefWatcher refWatcher = CheflyApplication.getRefWatcher(this);
refWatcher.watch(this);
}
}
Aggregations