0

i want to send data to online web server and i created database ...and i can transfer data through website but when i try to send data from my android application ..i cannot able to send it...i don't know that can transfer data with out create web api ..????

this is my class....

public class RestFulWebservice extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.rest_ful_webservice);

    final Button GetServerData = (Button) findViewById(R.id.GetServerData);

    GetServerData.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // WebServer Request URL
            String serverURL = "http://bnncollege.org/self-funded-accounts-finance-sy.php";

            // Use AsyncTask execute Method To Prevent ANR Problem
            new LongOperation().execute(serverURL);
        }
    });

}

// Class with extends AsyncTask class
private class LongOperation extends AsyncTask<String, Void, Void> {

    // Required initialization

    private final HttpClient Client = new DefaultHttpClient();

    @Override
    protected void onProgressUpdate(Void... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    private String Content;
    private String Error = null;
    private ProgressDialog Dialog = new ProgressDialog(
            RestFulWebservice.this);
    String data = "";
    TextView uiUpdate = (TextView) findViewById(R.id.output);
    TextView jsonParsed = (TextView) findViewById(R.id.jsonParsed);
    int sizeData = 0;
    EditText serverText = (EditText) findViewById(R.id.serverText);

    protected void onPreExecute() {
        // NOTE: You can call UI Element here.

        // Start Progress Dialog (Message)

        Dialog.setMessage("Please wait..");
        Dialog.show();

        try {
            // Set Request parameter
            data += "&" + URLEncoder.encode("surname", "UTF-8") + "="
                    + serverText.getText();
            Log.i("server", data);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    // Call after onPreExecute method
    protected Void doInBackground(String... urls) {

        /************ Make Post Call To Web Server ***********/
        BufferedReader reader = null;

        // Send data
        try {

            // Defined URL where to send data
            URL url = new URL(urls[0]);

            // Send POST data request
            Log.i("url", url.toString());
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            conn.connect();

            OutputStreamWriter wr = new OutputStreamWriter(
                    conn.getOutputStream());
            wr.write(data);
            wr.flush();

            // Get the server response
            reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while ((line = reader.readLine()) != null) {
                // Append server response in string
                sb.append(line + "\n");
            }

            // Append Server Response To Content String
            Content = sb.toString();
        } catch (Exception ex) {
            Error = ex.getMessage();
        } finally {
            try {

                reader.close();
            }

            catch (Exception ex) {
            }
        }

        /*****************************************************/
        return null;
    }

    protected void onPostExecute(Void unused) {
        // NOTE: You can call UI Element here.

        // Close progress dialog
        Dialog.dismiss();

        if (Error != null) {

            uiUpdate.setText("Output : " + Error);

        } else {

            // Show Response Json On Screen (activity)
            uiUpdate.setText(Content);

            /****************** Start Parse Response JSON Data *************/

            String OutputData = "";
            JSONObject jsonResponse;

            try {

                /******
                 * Creates a new JSONObject with name/value mappings from
                 * the JSON string.
                 ********/
                jsonResponse = new JSONObject(Content);

                /*****
                 * Returns the value mapped by name if it exists and is a
                 * JSONArray.
                 ***/
                /******* Returns null otherwise. *******/
                JSONArray jsonMainNode = jsonResponse
                        .optJSONArray("Android");

                /*********** Process each JSON Node ************/

                int lengthJsonArr = jsonMainNode.length();

                for (int i = 0; i < lengthJsonArr; i++) {
                    /****** Get Object for each JSON node. ***********/
                    JSONObject jsonChildNode = jsonMainNode
                            .getJSONObject(i);

                    /******* Fetch node values **********/
                    String name = jsonChildNode.optString("name")
                            .toString();
                    String number = jsonChildNode.optString("number")
                            .toString();
                    String date_added = jsonChildNode.optString(
                            "date_added").toString();

                    OutputData += " Name            : "
                            + name
                            + " \n "
                            + "Number       : "
                            + number
                            + " \n "
                            + "Time                 : "
                            + date_added
                            + " \n "
                            + "--------------------------------------------------\n";

                    // Log.i("JSON parse", song_name);
                }
                /****************** End Parse Response JSON Data *************/

                // Show Parsed Output on screen (activity)
                jsonParsed.setText(OutputData);

            } catch (JSONException e) {

                e.printStackTrace();
            }

        }
    }

}

}

this is link through that i can send data to webserver but i can not sent data from android app.....

http://bnncollege.org/self-funded-accounts-finance-sy.php

bhavdip
  • 189
  • 5
  • 20
  • i didn't see anything like send data to server in your code...why you use HttpClient here where as you write your network operation with oracle urlconnection..? – Ranjit Mar 07 '14 at 15:28
  • i updated my code..please review it...and i have to create webservice or without creating webservice i can send data to server... – bhavdip Mar 07 '14 at 15:40

0 Answers0