List of usage examples for com.squareup.okhttp Callback Callback
Callback
From source file:com.mummyding.app.leisure.database.cache.cache.ReadingCache.java
License:Open Source License
@Override public void load() { Utils.DLog("from net reading size: " + mList.size()); for (int i = 0; i < mUrls.length; i++) { String url = mUrls[i];//from w ww . j ava2 s . co m Request.Builder builder = new Request.Builder(); builder.url(url); Request request = builder.build(); HttpUtil.enqueue(request, new Callback() { @Override public void onFailure(Request request, IOException e) { mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE); } @Override public void onResponse(com.squareup.okhttp.Response response) throws IOException { if (response.isSuccessful() == false) { mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE); return; } ArrayList<String> collectionTitles = new ArrayList<String>(); for (int i = 0; i < mList.size(); i++) { if (mList.get(i).getIs_collected() == 1) { collectionTitles.add(mList.get(i).getTitle()); } } Gson gson = new Gson(); BookBean[] bookBeans = gson.fromJson(response.body().string(), ReadingBean.class).getBooks(); mList.clear(); for (BookBean bookBean : bookBeans) { mList.add(bookBean); } for (String title : collectionTitles) { for (int i = 0; i < mList.size(); i++) { if (title.equals(mList.get(i).getTitle())) { mList.get(i).setIs_collected(1); } } } mHandler.sendEmptyMessage(CONSTANT.ID_SUCCESS); } }); } }
From source file:com.mummyding.app.leisure.database.cache.cache.ScienceCache.java
License:Open Source License
@Override public void load() { Request.Builder builder = new Request.Builder(); builder.url(mUrl);//from w w w . ja v a 2 s. c o m Request request = builder.build(); HttpUtil.enqueue(request, new Callback() { @Override public void onFailure(Request request, IOException e) { mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE); } @Override public void onResponse(com.squareup.okhttp.Response response) throws IOException { if (response.isSuccessful() == false) { mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE); return; } ArrayList<String> collectionTitles = new ArrayList<String>(); for (int i = 0; i < mList.size(); i++) { if (mList.get(i).getIs_collected() == 1) { collectionTitles.add(mList.get(i).getTitle()); } } mList.clear(); Gson gson = new Gson(); ArticleBean[] articleBeans = (gson.fromJson(response.body().string(), ScienceBean.class)) .getResult(); for (ArticleBean articleBean : articleBeans) { mList.add(articleBean); } for (String title : collectionTitles) { for (int i = 0; i < mList.size(); i++) { if (title.equals(mList.get(i).getTitle())) { mList.get(i).setIs_collected(1); } } } mHandler.sendEmptyMessage(CONSTANT.ID_SUCCESS); } }); }
From source file:com.mummyding.app.leisure.ui.about.AboutFragment.java
License:Open Source License
@Override public boolean onPreferenceClick(Preference preference) { if (mAppIntro == preference) { Intent toIntro = new Intent(getActivity(), AppIntroActivity.class); startActivity(toIntro);/*from ww w. ja v a 2 s. co m*/ } else if (mDemoVideo == preference) { Intent toVideo = new Intent(getActivity(), DemoVideoActivity.class); startActivity(toVideo); } else if (mCheckUpdate == preference) { progressBar.setVisibility(View.VISIBLE); Request.Builder builder = new Request.Builder(); builder.url(CONSTANT.VERSION_URL); Request request = builder.build(); HttpUtil.enqueue(request, new Callback() { @Override public void onFailure(Request request, IOException e) { Snackbar.make(getView(), R.string.hint_fail_check_update, Snackbar.LENGTH_SHORT).show(); handle.sendEmptyMessage(1); } @Override public void onResponse(Response response) throws IOException { String latestVersion = response.body().string(); if (CONSTANT.CURRENT_VERSION.equals(latestVersion.trim())) { Snackbar.make(getView(), getString(R.string.notify_current_is_latest), Snackbar.LENGTH_SHORT).show(); } else { Snackbar.make(getView(), getString(R.string.notify_find_new_version) + latestVersion, Snackbar.LENGTH_SHORT).show(); } handle.sendEmptyMessage(1); } }); } else if (mStarProject == preference) { Utils.copyToClipboard(getView(), getString(R.string.project_url)); } else if (mShare == preference) { Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.text_share_info)); startActivity(Intent.createChooser(sharingIntent, getString(R.string.text_share_leisure))); } else if (mBlog == preference) { Utils.copyToClipboard(getView(), getString(R.string.author_blog)); } else if (mGitHub == preference) { Utils.copyToClipboard(getView(), getString(R.string.author_github)); } else if (mEmail == preference) { Utils.copyToClipboard(getView(), getString(R.string.author_email)); } return false; }
From source file:com.mummyding.app.leisure.ui.daily.DailyDetailsActivity.java
License:Open Source License
@Override protected void onDataRefresh() { Request.Builder builder = new Request.Builder(); builder.url(url);// w w w. ja va2 s . com Request request = builder.build(); HttpUtil.enqueue(request, new Callback() { @Override public void onFailure(Request request, IOException e) { handler.sendEmptyMessage(CONSTANT.ID_FAILURE); } @Override public void onResponse(Response response) throws IOException { String res = response.body().string(); Utils.DLog(res); Gson gson = new Gson(); dailyDetailsBean = gson.fromJson(res, DailyDetailsBean.class); cache.execSQL(DailyTable.updateBodyContent(DailyTable.NAME, dailyDetailsBean.getTitle(), dailyDetailsBean.getBody())); cache.execSQL(DailyTable.updateBodyContent(DailyTable.COLLECTION_NAME, dailyDetailsBean.getTitle(), dailyDetailsBean.getBody())); cache.execSQL(DailyTable.updateLargePic(DailyTable.NAME, dailyDetailsBean.getTitle(), dailyDetailsBean.getImage())); cache.execSQL(DailyTable.updateLargePic(DailyTable.COLLECTION_NAME, dailyDetailsBean.getTitle(), dailyDetailsBean.getImage())); imageUrl = dailyDetailsBean.getImage(); body = dailyDetailsBean.getBody(); handler.sendEmptyMessage(CONSTANT.ID_SUCCESS); } }); }
From source file:com.nizlumina.utils.WebUnitMaster.java
License:Open Source License
/** * Enqueue a request to the given URL and receive the response body in a String object via a listener * * @param url The URL for the request * @param listener Optional listener to retrieve the string object of the received response body. Check for null. This runs on the original thread that first calls the method. * @throws java.io.IOException/*from w ww . j a v a 2 s . co m*/ */ public synchronized void enqueueGetString(final String url, final WebUnitStringListener listener) throws IOException { executeAsync(url, new Callback() { @Override public void onFailure(Request request, IOException e) { if (listener != null) listener.onFailure(); } @Override public void onResponse(final Response response) throws IOException { final String finalResponse = response.body().string(); if (listener != null) listener.onFinish(finalResponse); } }); }
From source file:com.nuvolect.securesuite.webserver.Comm.java
License:Open Source License
public static void sendPostUi(final Context ctx, String url, final Map<String, String> params, final CommPostCallbacks cb) { String postBody = ""; String amphersand = ""; OkHttpClient okHttpClient = WebService.getOkHttpClient(); for (Map.Entry<String, String> entry : params.entrySet()) { postBody += amphersand;// ww w .j a v a 2 s. com postBody += entry.getKey(); postBody += "="; postBody += entry.getValue(); amphersand = "&"; } Request request = new Request.Builder().url(url).post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody)) .header(CConst.SEC_TOK, CrypServer.getSecTok()).build(); okHttpClient.newCall(request).enqueue(new Callback() { Handler mainHandler = new Handler(ctx.getMainLooper()); @Override public void onFailure(Request request, IOException e) { LogUtil.log("okHttpClient.netCall.onFailure"); LogUtil.logException(LogUtil.LogType.REST, e); } @Override public void onResponse(Response response) throws IOException { final String responseStr; final boolean success = response.isSuccessful(); if (success) responseStr = response.body().string(); else responseStr = response.message(); mainHandler.post(new Runnable() { @Override public void run() { if (!success) { if (cb != null) cb.fail("unexpected code " + responseStr); return; } if (cb != null) cb.success(responseStr); } }); } }); }
From source file:com.rsu.nuttanun.testdrivingvlicense.ConfirmScoreActivity.java
public void clickOKConfirm(View view) { String urlPHP = "http://swiftcodingthai.com/toey/add_score.php"; OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("id_login", loginStrings[0]) .add("Date", dateString).add("Score", scoreString).build(); Request.Builder builder = new Request.Builder(); Request request = builder.url(urlPHP).post(requestBody).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override//www . java2s .co m public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { Intent intent = new Intent(ConfirmScoreActivity.this, ScoreListView.class); intent.putExtra("login", loginStrings); startActivity(intent); finish(); } }); }
From source file:com.rsu.nuttanun.testdrivingvlicense.ScoreListView.java
private void createListView() { String urlPHP = "http://swiftcodingthai.com/toey/get_score_where.php"; OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("id_login", loginStrings[0]) .build();// w w w .j av a2 s .c om Request.Builder builder = new Request.Builder(); Request request = builder.url(urlPHP).post(requestBody).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { String strResponse = response.body().string(); Log.d("10AugV2", "strResponse ==>" + strResponse); try { JSONArray jsonArray = new JSONArray(strResponse); String[] dateStrings = new String[jsonArray.length()]; String[] scoreStrings = new String[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); dateStrings[i] = jsonObject.getString("Date"); scoreStrings[i] = jsonObject.getString("Score") + " ?"; } //for CoaurseAdapter coaurseAdapter = new CoaurseAdapter(ScoreListView.this, 1, dateStrings, scoreStrings); listView.setAdapter(coaurseAdapter); } catch (Exception e) { e.printStackTrace(); } } // onRespose }); }
From source file:com.rsu.nuttanun.testdrivingvlicense.SignUpActivity.java
private void upLoadNewUser() { OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("Name", nameString) .add("Surname", surnameString).add("Age", ageString).add("User", userString) .add("Password", passwordString).build(); Request.Builder builder = new Request.Builder(); Request request = builder.url(urlPHP).post(requestBody).build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override// w ww. j a v a 2 s . co m public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { finish(); } }); }
From source file:com.saiyanstudio.groceryassistant.RecipeActivity.java
private void initializeData() { //food2fork api //spoonacularFridgeAPIURL = "http://food2fork.com/api/search?key=eae722f5d2be7fd8fb1a0cd6a9310dbd&q=shredded%20chicken"; spoonacularFridgeAPIURL = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients?ingredients="; String ingredientListString = ""; for (int j = 0; j < ingredientList.size(); j++) { ingredientListString = ingredientListString + ingredientList.get(j).toLowerCase().replace(" ", "+"); if (j != (ingredientList.size() - 1)) ingredientListString += "%2C"; }/*from w w w .java 2 s . c om*/ //ingredientListString = "apples%2Cflour%2Csugar"; spoonacularFridgeAPIURL += ingredientListString; spoonacularFridgeAPIURL += "&limitLicense=false&number=10&ranking=1"; Log.i("spoonacularFridgeAPIURL", spoonacularFridgeAPIURL); if (isNetworkAvailable()) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(spoonacularFridgeAPIURL) .addHeader("X-Mashape-Key", "SDS3P5crHImshhb4jdBQayo4b77hp1TvcMgjsnNMrYbE6MZuYI") .addHeader("Accept", "application/json").build(); /* OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(spoonacularFridgeAPIURL) .build(); */ Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { RecipeActivity.this.runOnUiThread(new Runnable() { @Override public void run() { recipeProgressBar.setVisibility(View.GONE); } }); try { recipeJsonData = response.body().string(); Log.v("RECIPE_ACTIVITY", recipeJsonData); if (response.isSuccessful()) { JSONArray recipeArray = new JSONArray(recipeJsonData); Log.d("RECIPE_ACTIVITY", "recipeArray.length() : " + recipeArray.length()); for (int i = 0; i < recipeArray.length(); i++) { JSONObject recipeData = recipeArray.getJSONObject(i); Log.d("RECIPE_ACTIVITY", "Recipe Name : " + recipeData.getString("title")); RecipeItem recipeItem = new RecipeItem(recipeData.getString("title"), recipeData.getString("image"), recipeData.getInt("likes"), recipeData.getInt("usedIngredientCount"), recipeData.getInt("usedIngredientCount")); //RecipeItem recipeItem = new RecipeItem(recipeData.getString("title"),recipeData.getString("image_url"),3,(int)recipeData.getDouble("social_rank")); recipeItems.add(recipeItem); } RecipeActivity.this.runOnUiThread(new Runnable() { @Override public void run() { if (recipeItems.size() == 0) noRecipeMsg.setVisibility(View.VISIBLE); else noRecipeMsg.setVisibility(View.INVISIBLE); adapter.notifyDataSetChanged(); } }); } else { RecipeActivity.this.runOnUiThread(new Runnable() { @Override public void run() { noRecipeMsg.setVisibility(View.VISIBLE); //Toast.makeText(RecipeActivity.this, "Sorry no recipes available", Toast.LENGTH_SHORT).show(); } }); } } catch (IOException e) { Log.e("RECIPE_ACTIVITY", "Exception caught: ", e); } catch (JSONException e) { Log.e("RECIPE_ACTIVITY", "Exception caught: ", e); } } }); } else { Snackbar.make(linearLayout_recipe, "Network Unavialable", Snackbar.LENGTH_INDEFINITE).show(); //Toast.makeText(RecipeActivity.this,"Network Unavialable", Toast.LENGTH_SHORT).show(); } }