我正在开发一个 Android 代码,点击一个按钮就可以通过网络服务将数据插入远程数据库。我的 Web 服务代码非常简单。它只接受一个参数,即 android Spinner 值并将该值插入数据库
我已使用 HttpClient 发帖,但无法成功完成我的任务。谁能帮我解决这个问题?
这是网络服务代码:
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";
}
}
}
这是我的 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://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();
}
}
这是日志:
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
最佳答案
您很可能缺少这些行:(在 httpclient.execute 之前添加它们)
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json"); OR
httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
关于android - 通过 Web 服务将数据从 Android 应用程序输入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8033681/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是