如何在.net Web服务中接收json对象

时间:2013-03-06 18:32:01

标签: android asp.net sql-server json web-services

我的android逻辑如下:

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://localhost:8425/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();

     }

}   

.net webservices代码:

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
      {

[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              
// [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";
}

   }
 }

请给我解决方案 我想将数据从Android应用程序发送到.net Web服务,而.net webservices将我的数据存储在sql server 2008中 并且还提供了如何在.net webservices中接收json数据odject和json数据的解决方案。

0 个答案:

没有答案