Search in sources :

Example 1 with JsonArrayRequest

use of com.android.volley.toolbox.JsonArrayRequest in project Space-Station-Tracker by Kiarasht.

the class MapsActivity method updatePolyline.

/**
     * Update polyline sets the future predictions of ISS's position for up to 90 minutes.
     */
private void updatePolyline(Date currentDate) {
    long currentLong = currentDate.getTime() / 1000;
    final long[] futureTen = new long[10];
    for (int i = 0; i < futureTen.length; ++i) {
        futureTen[i] = currentLong + (30 * mPoly++);
    }
    final StringBuilder urlBuilder = new StringBuilder();
    for (long aFutureTen : futureTen) {
        urlBuilder.append(aFutureTen).append(",");
    }
    urlBuilder.setLength(urlBuilder.length() - 1);
    //TODO: As a user, I would like the option of changing the units from metric to imperial
    final String units = "miles";
    final String url = "https://api.wheretheiss.at/v1/satellites/25544/positions?timestamps=" + urlBuilder.toString() + "&units=" + units;
    final int finalStart = mPoly;
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            try {
                final LatLng[] latLngs = new LatLng[10];
                for (int i = 0; i < response.length(); ++i) {
                    latLngs[i] = new LatLng(response.getJSONObject(i).getDouble("latitude"), response.getJSONObject(i).getDouble("longitude"));
                }
                MapsActivity.this.runOnUiThread(new Runnable() {

                    public void run() {
                        if (finalStart == 10) {
                            for (int i = 0; i < futureTen.length - 1; ++i) {
                                mPolyLine = mMap.addPolyline(new PolylineOptions().add(latLngs[i], latLngs[i + 1]).width(mCurrentWidth).color(mCurrentColor));
                                mPolyArray[mPolyCounter++] = mPolyLine;
                            }
                            mLast = latLngs[latLngs.length - 1];
                            ++mProgress;
                        } else {
                            mPolyArray[mPolyCounter++] = mMap.addPolyline(new PolylineOptions().add(mLast, latLngs[0]).width(mCurrentWidth).color(mCurrentColor));
                            for (int i = 0; i < futureTen.length - 1; ++i) {
                                mPolyArray[mPolyCounter++] = mMap.addPolyline(new PolylineOptions().add(latLngs[i], latLngs[i + 1]).width(mCurrentWidth).color(mCurrentColor));
                            }
                            mLast = latLngs[latLngs.length - 1];
                            ++mProgress;
                        }
                    }
                });
            } catch (Exception e) {
                if (mLast == null) {
                    FirebaseCrash.report(new Exception("mLast was null"));
                }
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError e) {
        }
    });
    mRequestQueue.add(jsonArrayRequest);
}
Also used : VolleyError(com.android.volley.VolleyError) JsonArrayRequest(com.android.volley.toolbox.JsonArrayRequest) JSONArray(org.json.JSONArray) PolylineOptions(com.google.android.gms.maps.model.PolylineOptions) Response(com.android.volley.Response) LatLng(com.google.android.gms.maps.model.LatLng)

Example 2 with JsonArrayRequest

use of com.android.volley.toolbox.JsonArrayRequest in project EC2018App by Manan-YMCA.

the class QuestionFragment method getQuestion.

public void getQuestion() {
    JSONArray jsonArray = new JSONArray();
    JSONObject params = new JSONObject();
    try {
        params.put("fid", currFbid);
        if (Profile.getCurrentProfile() != null) {
            params.put("skey", mContext.getResources().getString(R.string.skey));
            params.put("fname", Profile.getCurrentProfile().getFirstName());
            params.put("lname", Profile.getCurrentProfile().getLastName());
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    jsonArray.put(params);
    jobReq = new JsonArrayRequest(Request.Method.POST, queURL, jsonArray, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            bar.setVisibility(View.GONE);
            JSONObject resp;
            refreshButton.setVisibility(View.GONE);
            try {
                resp = response.getJSONObject(0);
                try {
                    String end = resp.getString("response");
                    contestEnd.setVisibility(View.VISIBLE);
                    contestEnd.setText("YOU HAVE SUCCESSFULLY COMPLETED ALL THE QUESTIONS.\n WE WILL ANNOUNCE THE WINNERS ON 7th APRIL, 2018.\nIF YOU HAVE WON, WE WILL CONTACT YOU SHORTLY");
                    refreshButton.setVisibility(View.GONE);
                } catch (Exception e) {
                    bar.setVisibility(View.GONE);
                    barImage.setVisibility(View.VISIBLE);
                    queLayout.setVisibility(View.VISIBLE);
                    String imgUrl = resp.getString("image");
                    String que = resp.getString("desc");
                    Integer level = resp.getInt("pk");
                    question.setText(que);
                    QuestionFragment.this.level.setText("Level " + Integer.toString(level));
                    Picasso.with(mContext).load("https://xunbao-1.herokuapp.com" + imgUrl).into(xunbaoimg, new Callback() {

                        @Override
                        public void onSuccess() {
                            barImage.setVisibility(View.GONE);
                        }

                        @Override
                        public void onError() {
                            barImage.setVisibility(View.GONE);
                        }
                    });
                }
            } catch (JSONException e) {
                bar.setVisibility(View.GONE);
                // progressBar.dismiss();
                refreshButton.setVisibility(View.VISIBLE);
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            // progressBar.dismiss();
            bar.setVisibility(View.GONE);
            refreshButton.setVisibility(View.VISIBLE);
            volleyError.printStackTrace();
        }
    });
    queue.add(stat);
}
Also used : VolleyError(com.android.volley.VolleyError) JsonArrayRequest(com.android.volley.toolbox.JsonArrayRequest) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) FacebookCallback(com.facebook.FacebookCallback) Callback(com.squareup.picasso.Callback) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONException(org.json.JSONException) FacebookException(com.facebook.FacebookException)

Example 3 with JsonArrayRequest

use of com.android.volley.toolbox.JsonArrayRequest in project MPW by shineangelic.

the class Utils method synchCurrenciesFromCoinmarketcap.

public static void synchCurrenciesFromCoinmarketcap(Context ctx, CurrencyEnum mCur) {
    try {
        // synch jason
        RequestFuture<JSONArray> future = RequestFuture.newFuture();
        JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, Constants.ETHER_STATS_URL, new JSONArray(), future, future);
        JSONClientSingleton.getInstance(ctx).addToRequestQueue(request);
        // this will block
        JSONArray response = future.get();
        Log.d(Constants.TAG, response.toString());
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        Type listType = new TypeToken<List<Ticker>>() {
        }.getType();
        List<Ticker> posts = gson.fromJson(response.toString(), listType);
        Ticker fnd = null;
        for (Ticker currency : posts) {
            if (mCur.name().equalsIgnoreCase(currency.getSymbol()) || mCur.toString().equalsIgnoreCase(currency.getName())) {
                fnd = currency;
            }
            // always save ETH
            if (CurrencyEnum.ETH.name().equalsIgnoreCase(currency.getSymbol())) {
                CryptoSharedPreferencesUtils.saveEthereumValues(currency, ctx);
            }
            // always save BTC
            if (CurrencyEnum.BTC.name().equalsIgnoreCase(currency.getSymbol())) {
                CryptoSharedPreferencesUtils.saveBtcValues(currency, ctx);
            }
        }
        CryptoSharedPreferencesUtils.saveEtherValues(fnd, ctx);
    } catch (Exception e) {
        Log.d(Constants.TAG, "ERROR DURING COINMARKETCAP: " + e.getMessage());
    }
}
Also used : JsonArrayRequest(com.android.volley.toolbox.JsonArrayRequest) Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) Ticker(it.angelic.mpw.model.jsonpojos.coinmarketcap.Ticker) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) List(java.util.List) IllegalFormatException(java.util.IllegalFormatException)

Example 4 with JsonArrayRequest

use of com.android.volley.toolbox.JsonArrayRequest in project mintube by imshyam.

the class MainActivity method onQueryTextChange.

@Override
public boolean onQueryTextChange(String newText) {
    if (newText.length() > 0) {
        newText = newText.replace(" ", "+");
        String url = "http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&client=firefox&q=" + newText;
        JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                try {
                    JSONArray jsonArraySuggestion = (JSONArray) response.get(1);
                    String[] suggestions = new String[10];
                    for (int i = 0; i < 10; i++) {
                        if (!jsonArraySuggestion.isNull(i)) {
                            suggestions[i] = jsonArraySuggestion.get(i).toString();
                        }
                    }
                    Log.d("Suggestions", Arrays.toString(suggestions));
                    // Cursor Adaptor
                    String[] columnNames = { "_id", "suggestion" };
                    MatrixCursor cursor = new MatrixCursor(columnNames);
                    String[] temp = new String[2];
                    int id = 0;
                    for (String item : suggestions) {
                        if (item != null) {
                            temp[0] = Integer.toString(id++);
                            temp[1] = item;
                            cursor.addRow(temp);
                        }
                    }
                    CursorAdapter cursorAdapter = new CursorAdapter(getApplicationContext(), cursor, false) {

                        @Override
                        public View newView(Context context, Cursor cursor, ViewGroup parent) {
                            return LayoutInflater.from(context).inflate(R.layout.search_suggestion_list_item, parent, false);
                        }

                        @Override
                        public void bindView(View view, Context context, Cursor cursor) {
                            final TextView suggest = (TextView) view.findViewById(R.id.suggest);
                            ImageView putInSearchBox = (ImageView) view.findViewById(R.id.put_in_search_box);
                            String body = cursor.getString(cursor.getColumnIndexOrThrow("suggestion"));
                            suggest.setText(body);
                            suggest.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    searchView.setQuery(suggest.getText(), true);
                                    searchView.clearFocus();
                                }
                            });
                            putInSearchBox.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    searchView.setQuery(suggest.getText(), false);
                                }
                            });
                        }
                    };
                    searchView.setSuggestionsAdapter(cursorAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("Tag", "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
    }
    return true;
}
Also used : Context(android.content.Context) VolleyError(com.android.volley.VolleyError) JsonArrayRequest(com.android.volley.toolbox.JsonArrayRequest) ViewGroup(android.view.ViewGroup) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) SearchView(android.support.v7.widget.SearchView) ImageView(android.widget.ImageView) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) MatrixCursor(android.database.MatrixCursor) WebResourceResponse(android.webkit.WebResourceResponse) Response(com.android.volley.Response) CursorAdapter(android.support.v4.widget.CursorAdapter) TextView(android.widget.TextView) ImageView(android.widget.ImageView)

Example 5 with JsonArrayRequest

use of com.android.volley.toolbox.JsonArrayRequest in project Habba18 by chiragsastry1996.

the class Gallery_activity method fetchImages.

private void fetchImages() {
    // To be replaced by Snackbar
    pDialog.setMessage("Downloading ...");
    // To be replaced by Snackbar
    pDialog.show();
    JsonArrayRequest req = new JsonArrayRequest(endpoint, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());
            // To be replaced by Snackbar
            pDialog.hide();
            images.clear();
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject object = response.getJSONObject(i);
                    Image image = new Image();
                    image.setName(object.getString("name"));
                    JSONObject url = object.getJSONObject("url");
                    // image.setSmall(url.getString("small"));
                    image.setMedium(url.getString("medium"));
                    // image.setLarge(url.getString("large")); Enable this later
                    image.setTimestamp(object.getString("timestamp"));
                    images.add(image);
                } catch (JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                }
            }
            mAdapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
            // To be replaced by Snackbar
            pDialog.dismiss();
        }
    });
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JsonArrayRequest(com.android.volley.toolbox.JsonArrayRequest) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Aggregations

JsonArrayRequest (com.android.volley.toolbox.JsonArrayRequest)17 JSONArray (org.json.JSONArray)17 VolleyError (com.android.volley.VolleyError)10 Response (com.android.volley.Response)9 NetworkResponse (com.android.volley.NetworkResponse)7 Test (org.junit.Test)6 JSONObject (org.json.JSONObject)4 Gson (com.google.gson.Gson)3 String (java.lang.String)3 HashMap (java.util.HashMap)3 JSONException (org.json.JSONException)3 GsonBuilder (com.google.gson.GsonBuilder)2 Ticker (it.angelic.mpw.model.jsonpojos.coinmarketcap.Ticker)2 Type (java.lang.reflect.Type)2 List (java.util.List)2 Context (android.content.Context)1 Cursor (android.database.Cursor)1 MatrixCursor (android.database.MatrixCursor)1 CursorAdapter (android.support.v4.widget.CursorAdapter)1 SearchView (android.support.v7.widget.SearchView)1