Search in sources :

Example 6 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project yellowmessenger-sdk by yellowmessenger.

the class XMPPService method createUser.

private void createUser(final String username) {
    if (creatingUser) {
        return;
    }
    try {
        creatingUser = true;
        String salt = "a04aa6a74e76bf8f57b0e2e715138171";
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update((username + salt).getBytes());
        byte[] byteData = md.digest();
        // convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        String hash = sb.toString();
        String url = "https://sso.botplatform.io/createUser";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", username);
        jsonObject.put("hash", hash);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getBoolean("success") && PreferencesManager.getInstance(getBaseContext()).getXMPPUser() == null) {
                        JSONObject data = response.getJSONObject("data");
                        PreferencesManager.getInstance(getBaseContext()).setXMPPUser(new XMPPUser(data.getString("username"), data.getString("password")));
                        creatingUser = false;
                        XMPPService.this.login();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    creatingUser = false;
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                creatingUser = false;
            }
        });
        jsonObjectRequest.setRetryPolicy(retryPolicy);
        if (PreferencesManager.getInstance(getBaseContext()).getXMPPUser() == null) {
            queue.add(jsonObjectRequest);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : VolleyError(com.android.volley.VolleyError) SmackException(org.jivesoftware.smack.SmackException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ChatResponse(com.yellowmessenger.sdk.models.ChatResponse) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) XMPPUser(com.yellowmessenger.sdk.models.XMPPUser) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) MessageDigest(java.security.MessageDigest)

Example 7 with JsonObjectRequest

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

the class KratosInstrumentedTest method testJsonWalletRequest.

@Test
public void testJsonWalletRequest() throws Exception {
    final GsonBuilder builder = new GsonBuilder();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, Utils.getWalletStatsUrl(sharedPreferences) + minerAddr, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(final JSONObject response) {
            Log.i(Constants.TAG, response.toString());
            Gson gson = builder.create();
            // Register an adapter to manage the date types as long values
            Wallet retrieved = gson.fromJson(response.toString(), Wallet.class);
            assertNotNull(retrieved);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(Constants.TAG, "Error: " + error.getMessage());
            fail(error.getMessage());
        }
    });
    // Adding request to request queue
    JSONClientSingleton.getInstance(appContext).addToRequestQueue(jsonObjReq);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) GsonBuilder(com.google.gson.GsonBuilder) Wallet(it.angelic.mpw.model.jsonpojos.wallet.Wallet) Gson(com.google.gson.Gson) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Example 8 with JsonObjectRequest

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

the class KratosInstrumentedTest method testJsonBlockRequest.

@Test
public void testJsonBlockRequest() throws Exception {
    final GsonBuilder builder = new GsonBuilder();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, Utils.getBlocksURL(sharedPreferences), null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(final JSONObject response) {
            Log.i(Constants.TAG, response.toString());
            Gson gson = builder.create();
            // Register an adapter to manage the date types as long values
            Block retrieved = gson.fromJson(response.toString(), Block.class);
            assertNotNull(retrieved);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(Constants.TAG, "Error: " + error.getMessage());
            fail(error.getMessage());
        }
    });
    // Adding request to request queue
    JSONClientSingleton.getInstance(appContext).addToRequestQueue(jsonObjReq);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Block(it.angelic.mpw.model.jsonpojos.blocks.Block) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Example 9 with JsonObjectRequest

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

the class MainActivity method issueRefresh.

private void issueRefresh(final PoolDbHelper mDbHelper, final GsonBuilder builder) {
    Log.i(Constants.TAG, "JsonObjectRequest for: " + Utils.getHomeStatsURL(PreferenceManager.getDefaultSharedPreferences(MainActivity.this)));
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, Utils.getHomeStatsURL(PreferenceManager.getDefaultSharedPreferences(MainActivity.this)), null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(final JSONObject response) {
            Log.d(Constants.TAG, response.toString());
            hashText.post(new Runnable() {

                @Override
                public void run() {
                    Gson gson = builder.create();
                    // Register an adapter to manage the date types as long values
                    HomeStats retrieved = gson.fromJson(response.toString(), HomeStats.class);
                    mDbHelper.logHomeStats(retrieved);
                    // dati semi grezzi
                    int radioButtonID = radioGroupBackTo.getCheckedRadioButtonId();
                    View radioButton = findViewById(radioButtonID);
                    storia = mDbHelper.getHistoryData(BackToEnum.valueOf((String) radioButton.getTag()));
                    updateCurrentStats();
                    final RadioButton radioDay = findViewById(R.id.radioButtonDay);
                    final RadioButton radioMin = findViewById(R.id.radioButtonMinutes);
                    GranularityEnum granoEnum = GranularityEnum.HOUR;
                    if (radioDay.isChecked())
                        granoEnum = GranularityEnum.DAY;
                    else if (radioMin.isChecked())
                        granoEnum = GranularityEnum.MINUTE;
                    ChartUtils.drawDifficultyHistory(textViewNetDiffTitle, PoolQueryGrouper.groupAvgQueryResult(storia, granoEnum), (LineView) findViewById(R.id.line_view_difficulty), granoEnum);
                    ChartUtils.drawHashrateHistory(hashText, PoolQueryGrouper.groupAvgQueryResult(storia, granoEnum), (LineView) findViewById(R.id.line_view_hashrate), granoEnum);
                }
            });
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(Constants.TAG, "Error: " + error.getMessage());
            Snackbar.make(findViewById(android.R.id.content), "Network Error", Snackbar.LENGTH_SHORT).show();
            // prevent stale data appear
            updateCurrentStats();
        }
    });
    /*
        JsonArrayRequest jsonArrayCurrenciesReq = new JsonArrayRequest(Request.Method.GET,
                Constants.ETHER_STATS_URL, null,
                new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(final JSONArray response) {
                        Log.d(Constants.TAG, response.toString());
                        hashText.post(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = builder.create();
                                Log.d(Constants.TAG, response.toString());
                                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, MainActivity.this);
                                    }
                                    //always save BTC
                                    if (CurrencyEnum.BTC.name().equalsIgnoreCase(currency.getSymbol())) {
                                        CryptoSharedPreferencesUtils.saveBtcValues(currency, MainActivity.this);
                                    }
                                }
                                //eventually resets  when fnd = null
                                CryptoSharedPreferencesUtils.saveEtherValues(fnd, MainActivity.this);
                            }
                        });
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(Constants.TAG, "Error: " + error.getMessage());
                Crashlytics.logException(error);
            }
        });*/
    // Adding request to request queue
    JSONClientSingleton.getInstance(this).addToRequestQueue(jsonObjReq);
// JSONClientSingleton.getInstance(this).addToRequestQueue(jsonArrayCurrenciesReq);
}
Also used : VolleyError(com.android.volley.VolleyError) Gson(com.google.gson.Gson) HomeStats(it.angelic.mpw.model.jsonpojos.home.HomeStats) RadioButton(android.widget.RadioButton) NavigationView(android.support.design.widget.NavigationView) LineView(im.dacer.androidcharts.LineView) View(android.view.View) AdView(com.google.android.gms.ads.AdView) TextView(android.widget.TextView) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) GranularityEnum(it.angelic.mpw.model.db.GranularityEnum) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 10 with JsonObjectRequest

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

the class MinersActivity method fetchMinerStats.

private void fetchMinerStats(final MinerDBRecord rec) {
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Utils.getWalletStatsUrl(PreferenceManager.getDefaultSharedPreferences(this)) + rec.getAddress(), null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            // update known miners TABLE
            Log.d(Constants.TAG, response.toString());
            Gson gson = builder.create();
            // Register an adapter to manage the date types as long values
            final Wallet retrieved = gson.fromJson(response.toString(), Wallet.class);
            if (retrieved.getWorkersTotal() > (rec.getTopMiners() == null ? -1 : rec.getTopMiners()))
                rec.setTopMiners(retrieved.getWorkersTotal());
            if (retrieved.getCurrentHashrate() > (rec.getTopHr() == null ? -1 : rec.getTopHr()))
                rec.setTopHr(retrieved.getCurrentHashrate());
            rec.setPaid(retrieved.getStats().getPaid());
            try {
                // compute first paymt
                Payment pp = retrieved.getPayments().get(retrieved.getPayments().size() - 1);
                rec.setFirstSeen(pp.getTimestamp());
            } catch (Exception io) {
            // dont look back in anger
            }
            rec.setAvgHr(rec.getAvgHr() == null ? retrieved.getHashrate() : ((rec.getAvgHr() + retrieved.getHashrate()) / 2));
            // aggiorna UI
            rec.setLastSeen(retrieved.getStats().getLastShare());
            rec.setBlocksFound(retrieved.getStats().getBlocksFound());
            mDbHelper.updateMiner(rec);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e(Constants.TAG, "Error: " + error.getMessage());
        }
    });
    // Adding request to request queue
    JSONClientSingleton.getInstance(this).addToRequestQueue(jsonObjReq);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) Payment(it.angelic.mpw.model.jsonpojos.wallet.Payment) JSONObject(org.json.JSONObject) Wallet(it.angelic.mpw.model.jsonpojos.wallet.Wallet) Gson(com.google.gson.Gson) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Aggregations

JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)109 JSONObject (org.json.JSONObject)109 VolleyError (com.android.volley.VolleyError)97 Response (com.android.volley.Response)95 HashMap (java.util.HashMap)62 RequestQueue (com.android.volley.RequestQueue)59 User (model.User)50 JSONException (org.json.JSONException)45 Gson (com.google.gson.Gson)24 Context (android.content.Context)18 Test (org.junit.Test)18 Toast (android.widget.Toast)17 GsonBuilder (com.google.gson.GsonBuilder)17 NetworkResponse (com.android.volley.NetworkResponse)12 TextView (android.widget.TextView)9 DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)7 Wallet (it.angelic.mpw.model.jsonpojos.wallet.Wallet)7 HomeStats (it.angelic.mpw.model.jsonpojos.home.HomeStats)5 Intent (android.content.Intent)4 Pair (android.util.Pair)4