Search in sources :

Example 16 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project saga-android by AnandChowdhary.

the class Updater method checkForUpdates.

public static void checkForUpdates(final Context context, boolean visibility) {
    final int versionCode = getVersionCode(context);
    final ProgressDialog progressDialog = new ProgressDialog(context);
    String updateUrl = "https://www.dropbox.com/s/bka9o3p43oki217/saga.json?raw=1";
    if (visibility) {
        progressDialog.setTitle(context.getString(R.string.update));
        progressDialog.setMessage(context.getString(R.string.update_checking));
        progressDialog.setCancelable(true);
        progressDialog.show();
    }
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, updateUrl, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                int updateVersionCode = response.getInt("versionCode");
                if (updateVersionCode > versionCode && versionCode != 0) {
                    if (progressDialog.isShowing()) {
                        progressDialog.cancel();
                    }
                    final String apkUrl = response.getString("apkUrl");
                    String changelog = response.getString("changelog");
                    AlertDialog dialog = new AlertDialog.Builder(context).setTitle(context.getString(R.string.new_update)).setMessage(changelog).setPositiveButton(context.getString(R.string.update_now), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            File myFile = new File(Utils.getStoragePath(context), "update.apk");
                            if (myFile.exists())
                                myFile.delete();
                            Uri uri = Uri.parse(apkUrl);
                            DownloadManager dMgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                            DownloadManager.Request dr = new DownloadManager.Request(uri);
                            String filename = "update.apk";
                            dr.setTitle(context.getString(R.string.app_name) + " " + context.getString(R.string.update));
                            dr.setDestinationUri(Uri.fromFile(new File(Utils.getStoragePath(context) + "/" + filename)));
                            // dr.setDestinationInExternalPublicDir("/Saga/", filename);
                            dMgr.enqueue(dr);
                        }
                    }).setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    }).setCancelable(false).create();
                    dialog.show();
                } else {
                    if (progressDialog.isShowing()) {
                        progressDialog.cancel();
                        Toast.makeText(context, context.getString(R.string.no_update), Toast.LENGTH_SHORT).show();
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (progressDialog.isShowing()) {
                progressDialog.cancel();
                Toast.makeText(context, context.getString(R.string.error_connect), Toast.LENGTH_SHORT).show();
            }
        }
    });
    request.setShouldCache(false);
    VolleySingleton.getInstance(context).getRequestQueue().add(request);
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) VolleyError(com.android.volley.VolleyError) DialogInterface(android.content.DialogInterface) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Request(com.android.volley.Request) JSONException(org.json.JSONException) ProgressDialog(android.app.ProgressDialog) Uri(android.net.Uri) DownloadManager(android.app.DownloadManager) Response(com.android.volley.Response) JSONObject(org.json.JSONObject) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) File(java.io.File)

Example 17 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project TaEmCasa by Dionen.

the class JsonRequestCharsetTest method specifiedCharsetJsonObject.

@Test
public void specifiedCharsetJsonObject() throws Exception {
    byte[] data = jsonObjectString().getBytes(Charset.forName("ISO-8859-1"));
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=iso-8859-1");
    NetworkResponse network = new NetworkResponse(data, headers);
    JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
    Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
    assertNotNull(objectResponse);
    assertTrue(objectResponse.isSuccess());
    // don't check the text in Czech, ISO-8859-1 doesn't support some Czech characters
    assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) NetworkResponse(com.android.volley.NetworkResponse) String(java.lang.String) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Example 18 with JsonObjectRequest

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

the class JsonRequestCharsetTest method specifiedCharsetJsonObject.

@Test
public void specifiedCharsetJsonObject() throws Exception {
    byte[] data = jsonObjectString().getBytes(Charset.forName("ISO-8859-1"));
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=iso-8859-1");
    NetworkResponse network = new NetworkResponse(data, headers);
    JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
    Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
    assertNotNull(objectResponse);
    assertTrue(objectResponse.isSuccess());
    // don't check the text in Czech, ISO-8859-1 doesn't support some Czech characters
    assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) NetworkResponse(com.android.volley.NetworkResponse) String(java.lang.String) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Example 19 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project FastDev4Android by jiangqqlmj.

the class Fdv_JsonObjectRequest method post.

/**
 * 发送POST请求, 返回JSONObject对象数据
 * @param url    请求地址
 * @param listener  数据返回回调接口
 * @param params   POST请求参数
 */
public void post(String url, final Fdv_CallBackListener<JSONObject> listener, Map<String, String> params) {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (listener != null) {
                listener.onSuccessResponse(response);
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (listener != null) {
                listener.onErrorResponse(error);
            }
        }
    });
    addRequest(jsonObjectRequest, params);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 20 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project FastDev4Android by jiangqqlmj.

the class Fdv_JsonObjectRequest method get.

/**
 * 请求返回JSONObject对象 Get请求 无参数,或者get请求的参数直接拼接在URL上面
 * @param url   请求地址
 * @param listener  数据回调接口
 */
public void get(String url, final Fdv_CallBackListener<JSONObject> listener) {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (listener != null) {
                listener.onSuccessResponse(response);
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (listener != null) {
                listener.onErrorResponse(error);
            }
        }
    });
    addRequest(jsonObjectRequest);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) 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