Android Java Tips
===============================
    
********************************************************************************
## JSON
********************************************************************************
    new ReadJSONFeedTask().execute(url);


    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
            } else {
                Log.e("JSON", "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
********************************************************************************
## Json To Array
********************************************************************************
    try {
        JSONArray jsonArray = new JSONArray(result);
        ArrayList<String> list = new ArrayList<String>(); 
                
        if (jsonArray != null) { 
           int len = jsonArray.length();
           Log.i(" len : ", String.valueOf(len));
           for (int i=0;i<len;i++){ 
            list.add(jsonArray.get(i).toString());
           } 
    } 

    int index = 0;// Index of object in the ArrayList
    Log.i("Data",list.get(index).toString());//Preferred Method to get object
    Log.i("Data",list.toArray()[index].toString());
    

    
    JSONObject parentObject;
    try {
        parentObject = new JSONObject(result);
    ArrayList<String> list = new ArrayList<String>(); 
        
    JSONArray jsonArray;
    jsonArray = new JSONArray(parentObject.get("sale").toString());

    //Log.i(" UserSale : ",jsonArray.toString());
                    
    if (jsonArray != null) { 
       int len = jsonArray.length();
       Log.i(" len : ", String.valueOf(len));
       for (int i=0;i<len;i++){ 
        list.add(jsonArray.get(i).toString());
       } 
    }
    
********************************************************************************
## Print Long String in output log
********************************************************************************
    int length = result.length();

    for(int i=0; i<length; i+=1024)
    {
        if(i+1024<length)
            Log.d("JSON OUTPUT", result.substring(i, i+1024));
        else
            Log.d("JSON OUTPUT", result.substring(i, length));
    }
********************************************************************************
## List
********************************************************************************
    for (int i=0;i<len;i++){ 
        list.add(jsonArray.get(i).toString());
    } 


    int index = 0;// Index of object in the ArrayList
    Log.i("Data",list.get(index).toString());//Preferred Method to get object
    Log.i("Data",list.toArray()[index].toString());
    
********************************************************************************
## Alert Dialog
********************************************************************************
    private void openAlert(View view) {
         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
         
         alertDialogBuilder.setTitle(this.getTitle()+ " decision");
         alertDialogBuilder.setMessage("Are you sure?");
         // set positive button: Yes message
         alertDialogBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // go to a new activity of the app
                    Intent positveActivity = new Intent(getApplicationContext(),
                            PositiveActivity.class);
                    startActivity(positveActivity);    
                }
              });
         // set negative button: No message
         alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // cancel the alert box and put a Toast to the user
                    dialog.cancel();
                    Toast.makeText(getApplicationContext(), "You chose a negative answer", 
                            Toast.LENGTH_LONG).show();
                }
            });
         // set neutral button: Exit the app message
         alertDialogBuilder.setNeutralButton("Exit the app",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // exit the app and go to the HOME
                    MainActivity.this.finish();
                }
            });
         
         AlertDialog alertDialog = alertDialogBuilder.create();
         // show alert
         alertDialog.show();
    }
********************************************************************************
## Notify
********************************************************************************
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

    int icon = R.drawable.notification_icon;        
    CharSequence tickerText = "هشدار"; // ticker-text
    long when = System.currentTimeMillis();         
    Context context = getApplicationContext();     
    CharSequence contentTitle = "هشدار";  
    CharSequence contentText = "اکانت شما به زودی منقضی میشود، لطفا نسبت به شارژ اکانت خود اقدام فرمایید.";      
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    @SuppressWarnings("deprecation")
    Notification notification = new Notification(icon, tickerText, when);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    // and this
    final int HELLO_ID = 1;
    mNotificationManager.notify(HELLO_ID, notification);
********************************************************************************
## SharedPreferences
********************************************************************************

    SharedPreferences preferences = getApplicationContext().getSharedPreferences("net.pishgaman", Context.MODE_PRIVATE);

    String fullname = preferences.getString("fullname", "n/a");
    String mobile = preferences.getString("mobile", "n/a");
    String email = preferences.getString("email", "n/a");
    String current_service = preferences.getString("current_service", "n/a");
    String credit = preferences.getString("credit", "n/a");
    String deposit = preferences.getString("deposit", "n/a");
    String status = preferences.getString("status", "n/a");
    String jexpire = preferences.getString("jexpire", "n/a");
    String gexpire = preferences.getString("gexpire", "n/a");
    
    TextView fullname_TextView = (TextView)findViewById(R.id.fullname);
    fullname_TextView.setText(fullname);   
    TextView mobile_TextView = (TextView)findViewById(R.id.mobile);
    mobile_TextView.setText(mobile);   
    TextView email_TextView = (TextView)findViewById(R.id.email);
    email_TextView.setText(email);   
    TextView current_service_TextView = (TextView)findViewById(R.id.current_service);
    current_service_TextView.setText(current_service);
    TextView credit_TextView = (TextView)findViewById(R.id.credit);
    credit_TextView.setText(credit);
    TextView deposit_TextView = (TextView)findViewById(R.id.deposit);
    deposit_TextView.setText(deposit);
    TextView status_TextView = (TextView)findViewById(R.id.status);
    status_TextView.setText(status);
    TextView jexpire_TextView = (TextView)findViewById(R.id.jexpire);
    jexpire_TextView.setText(jexpire);
    //Log.i(" fullname  : ", fullname);
    //Log.i(" current_service : ", current_service);


    try {
        JSONObject parentObject = new JSONObject(result);
        
     // TODO Complete this Action!
        
        int userError = parentObject.getInt("error");
        if(userError == 0) {
            Log.i(" Now in :", " on ReadJson ");
            JSONObject user = parentObject.getJSONObject("info");   
            JSONObject userDetails = user.getJSONObject("user"); 
            String fullname = userDetails.getString("fullname"); 
            String mobile = userDetails.getString("mobile"); 
            String email = userDetails.getString("email");
            
            JSONObject serviceDetails = user.getJSONObject("service"); 
            String current_service = serviceDetails.getString("current_service");
            String credit = serviceDetails.getString("credit");
            String deposit = serviceDetails.getString("deposit");
            String status = serviceDetails.getString("status");
            String jexpire = serviceDetails.getString("jexpire");
            String gexpire = serviceDetails.getString("gexpire");

            SharedPreferences preferences =   getSharedPreferences("net.pishgaman", Context.MODE_PRIVATE);
            Editor edit = preferences.edit();
            edit.putString("fullname", fullname);
            edit.putString("mobile", mobile);
            edit.putString("email", email);
            edit.putString("current_service", current_service);
            edit.putString("credit", credit);
            edit.putString("deposit", deposit);
            edit.putString("status", status);
            edit.putString("jexpire", jexpire);
            edit.putString("gexpire", gexpire);
            edit.commit();                
            
               TextView fullname_TextView = (TextView)findViewById(R.id.fullname);
               fullname_TextView.setText(fullname);   
               TextView mobile_TextView = (TextView)findViewById(R.id.mobile);
               mobile_TextView.setText(mobile);   
               TextView email_TextView = (TextView)findViewById(R.id.email);
               email_TextView.setText(email);   
               TextView current_service_TextView = (TextView)findViewById(R.id.current_service);
               current_service_TextView.setText(current_service);
               TextView credit_TextView = (TextView)findViewById(R.id.credit);
               credit_TextView.setText(credit);
               TextView deposit_TextView = (TextView)findViewById(R.id.deposit);
               deposit_TextView.setText(deposit);
               TextView status_TextView = (TextView)findViewById(R.id.status);
               status_TextView.setText(status);
               TextView jexpire_TextView = (TextView)findViewById(R.id.jexpire);
               jexpire_TextView.setText(jexpire);
            
        } else {
            Toast.makeText(getApplicationContext(), "کاربری با اطلاعات وارد شده در سیستم موجود نمیباشد!", Toast.LENGTH_LONG).show();
            Log.i(" In Json :","userError is eccure!");
        }

        
        
        /*
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("JSON_RECEIVED_ACTION");
        main_context.sendBroadcast(broadcastIntent);
        */
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
_BY: Farid Ahmadian_  
_TAG: java, android, json_  
_DATE: 2014-08-27 19:36:53_