Wednesday, 27 August 2014

Android read and send json object using postmethod


first we need to set up the nesserr jar files after the follow this and do rules inside the manifesto file also

call this method at anyevent like

then call the spicemanager inside the activity


    SpiceManager spiceManager = new SpiceManager(JsonSpiceService.class);
    private String cachekey;
    private BitmapRequest imageRequest;
    private BitmapRequest imageRequest_child;

    @Override
    protected void onStart() {
        super.onStart();
        Logger.d(TAG, "starting service");
        spiceManager.start(this);
    }

    @Override
    protected void onStop() {
        if (spiceManager.isStarted()) {
            spiceManager.shouldStop();
            Logger.d(TAG, "stopping service");
        }
        super.onStop();

    }

performRequest(String user, String pass);


    private void performRequest(String user, String pass)
            throws FileNotFoundException {
        progress = new ProgressDialog(this);
        progress.setMessage("Installing Application....");
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        this.getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
        progress.setIndeterminate(true);
        progress.setCancelable(false);
        progress.show();
        **********.this.setProgressBarIndeterminateVisibility(true);
        ***********request = new PostRequest(
                ***************.this, user, pass);
        spiceManager.execute(request, "json", DurationInMillis.ONE_SECOND,
                new PostRequestListener(user, pass));
       
        Observation.setAdditional_notes("this is new note which must be take by the authur");
       
    }

take the postrequestlistern as insser class in the activity like where we can read the status of the either failure or success


// inner class of your spiced Activity
    private class PostRequestListener implements
            RequestListener<String> {
       PostRequestListener(String user, String pass) {
        }

        @Override
        public void onRequestFailure(SpiceException e) {
            progress.dismiss();

            Logger.d(TAG, "fail");
            Toast.makeText(getApplicationContext(), "Please Check Credential",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRequestSuccess(String response) {
            Toast.makeText(getApplicationContext(),
                    "succes request..... " + response, Toast.LENGTH_SHORT)
                    .show();
            progress.dismiss();
            Toast.makeText(getApplicationContext(), "succes request..... ",
                    Toast.LENGTH_SHORT).show();
            // finish();

        }
    }





take the separate class like for post request

==========================================================================



public class PostRequest extends SpringAndroidSpiceRequest<String> {
    Activity activity;
    Helper helper;
    DataBaseHelper helper2;
    WebService service;

    Message message = new Message();
    String url;

    String admin, pass;

    public ObservationPostRequest(Activity activity_, String admin_user,
            String admin_pass) {
        super(String.class);
        activity = activity_;
        admin = admin_user;
        pass = admin_pass;
        Logger.d("constructoe", "i came here");
    }

    @Override
    public String loadDataFromNetwork() throws Exception {

        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
        parameters.set("*******", "******");
        parameters.set("api_*******", "******");
    
       
        /*
         * HttpHeaders headers = new HttpHeaders();
         * headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
         * HttpEntity<MultiValueMap<String, String>> request = new
         * HttpEntity<MultiValueMap<String, String>>(parameters, headers);
         *
    


        Logger.e("information from the onservation ", ""+Observation.getAdditional_notes());
        RestTemplate restTemplate = new RestTemplate();
   
        try {
            // Add the Jackson and String message converters
            /*
             * restTemplate.getMessageConverters().add(new
             * MappingJackson2HttpMessageConverter());
             * restTemplate.getMessageConverters().add(new
             * StringHttpMessageConverter());
             */

            List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
            messageConverters.add(new FormHttpMessageConverter());
            messageConverters.add(new StringHttpMessageConverter());
            messageConverters.add(new MappingJacksonHttpMessageConverter());
            restTemplate.setMessageConverters(messageConverters);
        } catch (RestClientException e) {
            Log.e("===============", e.getMessage());
            Log.e("===============", "" + restTemplate.toString());
        }

        String response = restTemplate.postForObject(url, parameters,
                String.class);
        Logger.d("response", "**:" + parameters.toString());
        Logger.d("response", "**:" + response);
        return response;

    }

    /**
     * This method generates a unique cache key for this request. In this case
     * our cache key depends just on the keyword.
     *
     * @return
     */
    public String createCacheKey() {
        return "followers." + url;
    }
}




Thursday, 14 August 2014

Android Searchview,icons in the actionbar using menu itesm


ActionBar with Search Option and other options in Android.


ActionBar
ActionBar
ActionBar
ActionBar
First in your project “res” folder create a folder named “menu”. the create an xml inside this folder named “actions.xml”.
Now copy these to this xml.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?<span id="IL_AD4" class="IL_AD">xml version</span>="1.0" encoding="utf-8"?>
    <item android:id="@+id/action_search"
          android:icon="@android:drawable/ic_menu_search"
          android:title="Action Bar Search"
          android:showAsAction="ifRoom"
          android:actionViewClass="android.widget.SearchView" />
    <item android:id="@+id/action_add"
          android:icon="@android:drawable/ic_menu_add"
          android:title="Action Bar Add" />
    <item android:id="@+id/action_edit"
          android:icon="@android:drawable/ic_menu_edit"
          android:showAsAction="always"
          android:title="Action Bar Edit" />
    <item android:id="@+id/action_share"
          android:icon="@android:drawable/ic_menu_share"
          android:title="Action Bar Share"
          android:showAsAction="ifRoom" />
</menu>
OK now we go to the java code. In your Activity file copy this code.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<span id="IL_AD8" class="IL_AD">package</span> com.coderzheaven.actionbarsearch;
 
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;
 
public class ActionBarWithSearch extends Activity  implements OnQueryTextListener {
    TextView mSearchText;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSearchText = new TextView(this);
        mSearchText.setPadding(10, 10, 10, 10);
        mSearchText.setText("Action Bar Usage example from CoderzHeaven");
        setContentView(mSearchText);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actions, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setOnQueryTextListener(this);
        return true;
    }
 
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }
 
    @Override
    public boolean    onOptionsItemSelected       (MenuItem item) {
        Toast.makeText(this, "<span id="IL_AD3" class="IL_AD">Selected</span> Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
        return true;
    }
 
    // The following callbacks are called for the SearchView.OnQueryChangeListener
    public boolean onQueryTextChange(String newText) {
        newText = newText.isEmpty() ? "" : "Query so far: " + newText;
        mSearchText.setText(newText);
        mSearchText.setTextColor(Color.GREEN);
        return true;
    }
 
    public boolean      onQueryTextSubmit      (String query) {
        //Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
        mSearchText.setText("Searching for: " + query + "...");
        mSearchText.setTextColor(Color.RED);
        return true;
    }
}