-1

HI i created an application in that i hid edit text box and two buttons ,after i click button event that textbox will popup at run time ,it is working propely, but i need the typing text should pass to server, help me. All ideas are welcome

ragav
  • 69
  • 1
  • 2
  • 5

5 Answers5

3

If I'm understanding you correctly, you have text in a textbox, and you want to send that data to a server. You can look at one of these tutorials/snippets to see about posting your data to a web server:
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
or Secure HTTP Post in Android

Community
  • 1
  • 1
Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100
  • Hi i didn't get solution can u tel me anther solution . i want text passing from textbox to server – ragav Mar 09 '11 at 08:40
  • You need to elaborate more on what didn't work. Tell us what type of server you have, perhaps a bit more about what you did try – Dolan Antenucci Mar 09 '11 at 15:37
  • Hi, Now i can send message from (android mobile) client to (system) local server, but i want to implement sending text message from (android mobile) client to (android mobile) server. – ragav May 11 '11 at 04:32
  • both programs (client & server) running locally on same phone? and they are separate programs/processes? – Dolan Antenucci May 11 '11 at 06:53
  • Hi dolan,uyou are exactly correct client and server same phone in same application can u help me ,all ideas are welcom – ragav May 18 '11 at 10:48
1

Hi this code will send your data to server written in edittext. For textview just change the name of edittext by name of textview. Here is the code:

package com.example.asynchttppost;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    private EditText value;
    private Button btn;
    private ProgressBar pb;
        private TextView tv;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        value=(EditText)findViewById(R.id.editText1);
        btn=(Button)findViewById(R.id.button1);
        pb=(ProgressBar)findViewById(R.id.progressBar1);
                tv =(TextView)findViewById(R.id.TextView1);
        pb.setVisibility(View.GONE);
        btn.setOnClickListener(this);
    }


    public void onClick(View v) {
        // TODO Auto-generated method stub
            if(value.getText().toString().length()<1){

                // out of range
                Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
            }else{
                pb.setVisibility(View.VISIBLE);
                new MyAsyncTask().execute(value.getText().toString());      
            }


    } 

    private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

        @Override
        protected Double doInBackground(String... params) {
            // TODO Auto-generated method stub
            postData(params[0]);
            return null;
        }

        protected void onPostExecute(Double result){
            pb.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_LONG).show();
        }
        protected void onProgressUpdate(Integer... progress){
            pb.setProgress(progress[0]);
        }

        public void postData(String valueIWantToSend) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            //HttpPost httppost = new HttpPost("http://10.0.2.2/chotu/index.php");
            HttpPost httppost = new HttpPost("http://192.168.1.13/educlinic/Widget/AndroidApp");
            try {

                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

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

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

    }
}
Neha Mangla
  • 1,042
  • 3
  • 13
  • 24
1

Getting text from server

package com.example.sonasys.net;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.sonaprintersd.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class SingleContactActivity extends Activity {
    private static final String TAG_CONTACTS = "Contacts";
    private static final String TAG_POSTLINE = "PostLine";
    private static final String TAG_Post_Img = "Post_Img";
    private static final String TAG_Post_Img_O = "Post_Img_O";

    private static String url;
    TextView uid, pid;
    JSONArray contacts = null;
    private ProgressDialog pDialog;
    String details;
    // String imagepath = "http://test2.sonasys.net/Content/WallPost/b3.jpg";
    String imagepath = "";
    Bitmap bitmap;
    ImageView image;
    String imagepath2;

    ArrayList<HashMap<String, String>> contactList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_contact);

        url = "http://test2.sonasys.net/MobileApp/GetSinglePost?UserId="
                + uid.getText() + "&Post_ID=" + pid.getText();

        contactList = new ArrayList<HashMap<String, String>>();
        new GetContacts().execute();

    }

    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(SingleContactActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            // pDialog.setTitle("Post Details");
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    // looping through All Contacts

                    JSONObject c = contacts.getJSONObject(0);

                    details = c.getString(TAG_POSTLINE);
                    imagepath = c.getString(TAG_Post_Img);
                    imagepath2 = c.getString(TAG_Post_Img_O);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**/
            TextView Details = (TextView) findViewById(R.id.details);
            // Details.setText(details);
            Details.setText(android.text.Html.fromHtml(details));

        }
    }
khelwood
  • 55,782
  • 14
  • 81
  • 108
1

JSON or XML can be used for send data to remote server.

Swapna
  • 2,245
  • 2
  • 19
  • 22
0

ADD new class Service Handler

public class ServiceHandler {

    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;

    public ServiceHandler() {

    }

    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {



        return this.makeServiceCall(url, method, null);
    }

    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * 

     * */

    public String makeServiceCall(String url, int method,List<NameValuePair> params) {

        try {
            // http client

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {               
                    httpPost.setEntity(new UrlEncodedFormEntity(params));

                }

                httpResponse = httpClient.execute(httpPost);

            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);

                httpResponse = httpClient.execute(httpGet);

            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;

    }