0

I am building an android application where user enter the name, email ID This are parse to URL and all the user data are store there.

Now, When an user send this info an ID is generated at that Time on server and it resend to me that unique ID as the URL response.

How can I get this response and store it. So I can use it For my future use.

Here is my code for parsing this value to that URL -

private class DownloadJSON extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog

    }

    @Override
    protected String doInBackground(String... params) {

        // Create an array
        //public String postData() {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://URL/api/user");

          try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>2);
            nameValuePairs.add(new BasicNameValuePair("name", name));
            nameValuePairs.add(new BasicNameValuePair("email", email));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            InputStream parse = entity.getContent();

            return getStringFromInputStream(parse);

          } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
           } catch (IOException e) {
            // TODO Auto-generated catch block
          }

            return "No Result !";
        } 

         //convert InputStream to String
        private  String getStringFromInputStream(InputStream is) {

            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();

            String line;
            try {

                br = new BufferedReader(new InputStreamReader(is));
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            return sb.toString();
          }


    @Override
    protected void onPostExecute(String args) {

    }
}

I am new to android please help me!!

qwertymaster
  • 251
  • 1
  • 5
  • 21

1 Answers1

2

Better to use SharedPreferences to save all with a well known key.

First save your userName, EmailId in the sharedpreferences , something like

//Intialize it first
SharedPreferences sharedpreferences = getSharedPreferences("SOME_KEY", Context.MODE_PRIVATE);

//Use the Editor to put value inside this
Editor editor = sharedpreferences.edit();
//save both the name and email first 
editor.putString("NAME", name);
editor.putString("EMAILID", email);
//finally commit it
editor.commit();

Now do your server part...

@Override 
protected String doInBackground(String... params) {

    // Create an array 
    //public String postData() { 
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://URL/api/user");

      try { 
        // Add your data 
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>2);
        nameValuePairs.add(new BasicNameValuePair("name", name));
        nameValuePairs.add(new BasicNameValuePair("email", email));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request 
        HttpResponse response = httpclient.execute(httppost);
        String responseId = response.getEntity().getContent();

        //Just save your ID in the same sharedPreferences in UI Thread
        runOnUiThread(new Runnable(){
          public void run() { 
            Editor editor = sharedpreferences.edit();
            //save your ID now
           editor.putString("ID", responseId );
           editor.commit();
          } 
       }); 
       ..............................
       ..............................

Whenever you need it, just get it from your SharedPreference with the relevant key.like

      String id = sharedpreferences.getStringExtra("ID", "Some default value");
Ranjit
  • 4,797
  • 3
  • 28
  • 62
  • can you please help me to solving this problem - "Type mismatch: cannot convert from InputStream to String" in line - String responseId = response.getEntity().getContent(); – qwertymaster Nov 30 '14 at 11:06
  • @qwertymaster you can use any option from these solutions.. check it once http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string – Ranjit Dec 01 '14 at 05:48