Async task with GSON parsing

step 1:-   download below libs

https://drive.google.com/open?id=0B1dF4YSBqUooMWFZYUpUQ2MtRTQ
https://drive.google.com/open?id=0B1dF4YSBqUooc3I3bDYwZnllbDg
https://drive.google.com/open?id=0B1dF4YSBqUooZ3ZmUzVaREhCUVk


Step 2:-

compile 'com.google.code.gson:gson:2.2.4'

Step 3:- 

packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt' 
  exclude 'META-INF/LICENSE.txt'  
  exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE' 
   exclude 'META-INF/LICENSE' 
   exclude 'META-INF/DEPENDENCIES'  
  exclude 'META-INF/notice.txt' 
   exclude 'META-INF/license.txt'  
  exclude 'META-INF/dependencies.txt' 
   exclude 'META-INF/LGPL2.1'
}

Step 4:-

class MyAsync extends AsyncTask<String, Void, String> {

            @Override
            protected void onPreExecute() {
                ConstantData.displayProgress(BaseActivity.this);
            }

            @Override
            protected String doInBackground(String... urls) {
                String responceStr = "";
                try {

                    HttpParams httpParameters = new BasicHttpParams();
                    int timeoutConnection = 5000;
                    HttpConnectionParams.setConnectionTimeout(httpParameters,
                            timeoutConnection);
                    int timeoutSocket = 7000;
                    HttpConnectionParams
                            .setSoTimeout(httpParameters, timeoutSocket);

                    HttpClient httpClient = new DefaultHttpClient(httpParameters);
                    HttpPost httppost = new HttpPost(ServerUrl.cancelComplaint);

                    httppost.addHeader("Content-Type", "multipart/form-data");
                    httppost.setEntity(new StringEntity(postJson));
                    HttpResponse response = httpClient.execute(httppost);
                    if (response.getStatusLine().getStatusCode() == 200) {
                        HttpEntity entity = response.getEntity();
                        if (entity != null) {
                            responceStr = EntityUtils.toString(entity);

                        }
                    }
                    Log.i("res", responceStr);
                } catch (Exception e) {
                    responceStr = "error: " + e.toString();
                }
                Log.i("RESPONSE", responceStr);
                return responceStr;
            }

            @Override
            protected void onPostExecute(String result) {
   
     Gson gson = new GsonBuilder().setPrettyPrinting().create();
     final Loginresponce userObjectResponce = gson.fromJson(
       result, Loginresponce.class);

            }
        }
        new MyAsync().execute("");

Comments

Popular posts from this blog

Radio Button style

android wifi debugging without root android studio