Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6809117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:02:51+00:00 2026-05-26T20:02:51+00:00

I am working on an Android code that on click of a button inserts

  • 0

I am working on an Android code that on click of a button inserts data in a remote database via a web service. My web service code is pretty simple. It just takes in one parameter which is the android Spinner value and inserts that value into the database

I have used HttpClient for posting but I am not able to succeed in doing my task. Can anyone just help me out with this issue?

Here is the web service code:

           using System;
           using System.Collections;
           using System.ComponentModel;
           using System.Data;
           using System.Linq;
           using System.Web;
           using System.Web.Services;
           using System.Web.Services.Protocols;
           using System.Xml.Linq;
           using System.Data.SqlClient;


      namespace putdata
          {
        /// <summary>
      /// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public String put(String value)
    {
        int count=1;
        int rows;

        SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
        try
        {
            myConnection.Open();
            count++;
            SqlCommand myCommand = new SqlCommand();
            myCommand.Connection = myConnection;
            myCommand.CommandText = "insert into record values('"+ count +"','" + value + "')";
            myCommand.Parameters.Add("@value", SqlDbType.VarChar).Value = value;
            rows = myCommand.ExecuteNonQuery();
            SqlDataReader myReader = myCommand.ExecuteReader();

          }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            myConnection.Close();
        }

        return "success";
    }

       }
     }

Here is my main logic for the Android code:

           public void onClick(View v) {

        String spinnerval1="";
        String spinnerval2="";

        // Perform action on clicks
        Toast.makeText(Mark.this, "calling web service", Toast.LENGTH_LONG).show();
         if (v == findViewById(R.id.button)) {
             // prepare the dialog box
            Button progress = (Button) findViewById(R.id.button);
            progress.setOnClickListener((OnClickListener) Mark.this);

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/enterdata/Service1.asmx");


            try
            {
            String result;
            JSONObject obj = new JSONObject();
            obj.put("Maths",spinnerval1);
            obj.put("Science",spinnerval2);


            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("value1", spinnerval1));
            nameValuePairs.add(new BasicNameValuePair("value2", spinnerval2));
            httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));

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

            if (httpEntity != null) 
            {
                InputStream is = httpEntity.getContent();
                result = is.toString();
                Log.i("Result is ", "Result: " + result);
            }

            }

            catch (ClientProtocolException e1) 
            {
                // TODO Auto-generated catch block
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



            ProgressDialog dialog=new ProgressDialog(Mark.this);

             // make the progress bar cancelable
             dialog.setCancelable(true);

             // set a message text
             dialog.setMessage("Loading...");

             dialog.show();

         }

    }     

Here is the logcat:

    11-07 12:20:28.970: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example/.Mark }

    11-07 12:20:29.361: DEBUG/AndroidRuntime(299): Shutting down VM
    11-07 12:20:29.390: DEBUG/dalvikvm(299): Debugger has detached; object registry had 1 entries
    11-07 12:20:29.460: INFO/AndroidRuntime(299): NOTE: attach of thread 'Binder Thread #3' failed
    11-07 12:20:29.470: INFO/ActivityManager(58): Start proc com.example for activity com.example/.Mark: pid=306 uid=10060 gids={3003, 1015}
    11-07 12:20:31.170: INFO/ActivityManager(58): Displayed activity com.example/.Mark: 1809 ms (total 1809 ms)
   11-07 12:20:39.341: DEBUG/dalvikvm(127): GC_EXPLICIT freed 1321 objects / 73848 bytes in 161ms
   11-07 12:20:40.981: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45004b40
   11-07 12:20:44.440: DEBUG/dalvikvm(201): GC_EXPLICIT freed 109 objects / 9024 bytes in 190ms 
   11-07 12:20:48.840: INFO/Result is(306): Result: org.apache.http.conn.EofSensorInputStream@44f4a4f8
   11-07 12:20:50.490: DEBUG/dalvikvm(260): GC_EXPLICIT freed 759 objects / 54952 bytes in 1185ms
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:02:51+00:00Added an answer on May 26, 2026 at 8:02 pm

    u r missing these lines most probably : (add them before httpclient.execute)

    httppost.setHeader("Accept", "application/json");
         httppost.setHeader("Content-type", "application/json"); OR 
    
         httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code that i'm working on : <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
I'm working on an application that calls the ZXing scanner on button click. That
I am working on an Android app that utilizes the Google Maps API MapView,
I am working on an Android service. I need to call RecognizerIntent from a
I am working on an Android app that has multiple screens the user will
In my Android program, I have some code that downloads a file. This works
I'm working on an Android application that will send MMS internally without using the
I'm working with some Android code created by other developers, and they've asked me
OK, well I've been working on a simple app that allows users to save
Currently I am working on an Android application that is dynamically creating controls. Everytime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.