Json parsing using different Methods

Your code to where you implemented the Json parser

    
    /**
     * @author Mihir
     * @description AsyncTask for getting data from Services
     */
    public class JsonParsing extends AsyncTask<String, Void, Object[]>
    {
        ProgressDialog mProgressDialog = new ProgressDialog(Category.this);
        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
            mProgressDialog.setCancelable(false);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setMessage(Constant.MSG_PROGRESSBAR_WAITING);
            mProgressDialog.show();
        }

        @Override
        protected Object[] doInBackground(String... params) 
        {
            try 
            {
                String url = Constant.JSON_URL.URL_CATEGORY;
                return common.HTTP_POST(url, null, null);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(Object[] object) 
        {
            super.onPostExecute(object);
            mProgressDialog.dismiss();

            try 
            {
                if(object != null)
                { 
                    if((Integer)object[0] == 200)
                    {
                        String response = common.InputStreamToString(((InputStream)object[2]));
                        if(response != null)
                        {
                        }
                        else 
                        {
                            Toast.makeText(Category.this, Constant.MSG_SERVER_PROBLEM, Toast.LENGTH_SHORT).show();
                        }
                    }
                    else
                    {
                        Toast.makeText(Menu.this, ((String)object[1]), Toast.LENGTH_LONG).show();
//                        Common.errorDialogMsg(Category.this, (String)object[1]);
                    }
                }
                else
                {
                    Toast.makeText(Category.this, Constant.MSG_SERVER_PROBLEM, Toast.LENGTH_LONG).show();
                }
            }
            catch(Exception e) 
            {
                e.printStackTrace();
            }
        }
    }

Some useful constant strings

    
    public static final String HEADER_COOKIE = "Cookie";
    public static final String NETWORK_MESSAGE = "Please, Check your network connection";
    public static final String MSG_PROGRESSBAR_WAITING = "Please wait while loading...";
    public static final String MSG_SERVER_PROBLEM = "Server error occured.\nPlease, try again after some time.";

    /**
     * @author Mihir
     * @description DIALOG BOX BUTTON TEXT  
     */
    public static final class DIALOGBOX_BUTTON_TEXT 
    {
        public static final String BUTTON_YES = "YES";
        public static final String BUTTON_NO = "NO";
        public static final String BUTTON_OK = "OK";
        public static final String BUTTON_CANCEL = "Cancel";

        public static final String NETWORK_SETTING = "Network Settings";
        public static final String RETRY = "Retry";
    }

Code for inputstream to string

    
    /**
     * @author Mihir
     * @descriptions used to convert inputstream to string
     * @param inputstream
     * @return string value
     */
    public String InputStreamToString(InputStream inputstream) 
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        StringBuilder mStringBuilder = new StringBuilder();
        
        try 
        {
            if (inputstream != null) 
            {
                BufferedReader mBufferedReader = new BufferedReader(new InputStreamReader(inputstream), 8);
                String line = "";
                while ((line = mBufferedReader.readLine()) != null) 
                {
                    mStringBuilder.append(line + "\n");
                }
                inputstream.close();
                return mStringBuilder.toString();
            }
            return null;
            
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        }
    }

To get HTTP response code and response message

    
    /**
     * @author Mihir
     * @description use to get http response code
     * @param response
     * @return ResponseCode
     */
    public int getResponseStatusCode(HttpResponse response) 
    {
        StatusLine statusLine = response.getStatusLine();
        return statusLine.getStatusCode();
    }

    /**
     * @author Mihir
     * @description use to get http response code
     * @param response
     * @return ResponseMessage
     */
    public String getResponseStatusMessage(HttpResponse response) 
    {
        StatusLine statusLine = response.getStatusLine();
        return statusLine.getReasonPhrase().toString();
    }

HTTP Get method for Json parsing

    
    /**
     * @author Mihir
     * @description user to get json from URL
     * @param url
     * @param cookies
     * @return
     */
    public Object[] HTTP_GET(String url, String cookies) 
    {
        Object object[] = new Object[3];
        // DefaultHttpClient
        HttpClient mHttpClient = new DefaultHttpClient();
        try 
        {
            //Log.d("URL", url);
            HttpGet mHttpGet = new HttpGet(url);
            
            if (cookies != null) 
            {
                mHttpGet.setHeader(Constant.HEADER_COOKIE, cookies);
            }
            
            HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
            
            object[0] = getResponseStatusCode(mHttpResponse);
            object[1] = getResponseStatusMessage(mHttpResponse);
            
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            object[2] = mHttpEntity.getContent();
            
//            Log.d(TAG, "obj1 = " + object[0] + " obj2 = " + object[1] + " obj3 = " + object[2]);
            return object;
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        }
    }

HTTP Post method for Json parsing

    
    /**
     * @author Mihir 
     * @description use for post json to url 
     * @param context
     * @param URL
     * @param mNameValuePairs
     * @param cookies
     * @return post response
     */
    public Object[] HTTP_POST(String url, List<NameValuePair> mNameValuePairs, String cookies) 
    {
        Object object[] = new Object[3];
        HttpClient mHttpClient = new DefaultHttpClient();
        try 
        {
            //Log.d("URL", url);
            HttpPost mHttpPost = new HttpPost(url);

            if (cookies != null) 
            {
                mHttpPost.setHeader(Constant.HEADER_COOKIE, cookies);
            }
            
            if(mNameValuePairs!=null)
            {
                mHttpPost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
            }

            HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
        
            object[0] = getResponseStatusCode(mHttpResponse);
            object[1] = getResponseStatusMessage(mHttpResponse);
            
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            object[2] = mHttpEntity.getContent();
//            Log.d(TAG, "obj1 = " + object[0] + " obj2 = " + object[1] + " obj3 = " + object[2]);
            
            return object;
            /*if(getResponseStatusCode(mHttpResponse) == 406) 
            {
                Log.d("406 error", "" + getResponseStatusCode(mHttpResponse));
                HttpEntity mHttpEntity = mHttpResponse.getEntity();
                return mHttpEntity.getContent();
            }
            if(getResponseStatusCode(mHttpResponse) == 200) 
            {
                HttpEntity mHttpEntity = mHttpResponse.getEntity();
                return mHttpEntity.getContent();
            }
            else 
            {
                Log.d("Error", ""+getResponseStatusCode(mHttpResponse));
                return null;
            }*/
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        } 
    }

HTTP Put method for Json parsing

    
    /**
     * @author Mihir 
     * @description use for put json to url 
     * @param url
     * @param mNameValuePairs
     * @param cookies
     * @return put response
     * @throws Exception
     */
    public Object[] HTTP_PUT(String url,List<NameValuePair> mNameValuePairs, String cookies) throws Exception 
    {
        Object object[] = new Object[3];
        HttpClient mHttpClient = new DefaultHttpClient();
        
        try 
        {
            //Log.d("URL", url);
            HttpPut mHttpPut = new HttpPut(url);

            if (cookies != null) 
            {
                mHttpPut.setHeader(Constant.HEADER_COOKIE, cookies);
            }
            
            if(mNameValuePairs!=null)
            {
                mHttpPut.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
            }

            HttpResponse mHttpResponse = mHttpClient.execute(mHttpPut);

            object[0] = getResponseStatusCode(mHttpResponse);
            object[1] = getResponseStatusMessage(mHttpResponse);
            
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            object[2] = mHttpEntity.getContent();
//            Log.d(TAG, "obj1 = " + object[0] + " obj2 = " + object[1] + " obj3 = " + object[2]);
            
            return object;
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        }
    }

HTTP Delete method for Json parsing

    
    /**
     * @author Mihir 
     * @description use for delete record passing url with id
     * @param url
     * @param cookie
     * @return delete response
     */
    public Object[] HTTP_DELETE(String url, String cookies) 
    {
        Object object[] = new Object[3];
        HttpClient mHttpClient = new DefaultHttpClient();
        
        try 
        {
            //Log.d("URL", url);
            HttpDelete mHttpDelete = new HttpDelete(url);
            
            if (cookies != null) 
            {
                mHttpDelete.setHeader(Constant.HEADER_COOKIE, cookies);
            }
            
            HttpResponse mHttpResponse = mHttpClient.execute(mHttpDelete);
            
            object[0] = getResponseStatusCode(mHttpResponse);
            object[1] = getResponseStatusMessage(mHttpResponse);
            
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            object[2] = mHttpEntity.getContent();
//            Log.d(TAG, "obj1 = " + object[0] + " obj2 = " + object[1] + " obj3 = " + object[2]);

            return object;
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        }
    }

Comments

Popular Posts