我在连接到我创建的Web服务时遇到问题。 当我尝试从我的Android代码调用web服务时,它不起作用,但当我手动编写请求时,地址栏中传递的变量可以正常工作。 请看一下代码。我认为问题在于我在页面加载时调用的函数,当我通过Android调用它时它不起作用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using System.Web;
using System.Data.SqlClient;
using System.Collections;
namespace MyService
{
public partial class Caller : System.Web.UI.Page
{
//public int a, b;
protected void Page_Load(object sender, EventArgs e)
{
this.txtBox1.Text = Request.QueryString["Name"];
this.txtBox2.Text = Request.QueryString["Price"];
if (txtBox1.Text != "" && txtBox2.Text != "")
Add(txtBox1.Text, Convert.ToInt32(txtBox2.Text));
}
public void Add(String name, int price)
{
//this.txtBox1.Text = Request.QueryString["Name"];
//this.txtBox2.Text = Request.QueryString["Price"];
SqlConnection con = new SqlConnection(util.ConnectionString);
con.Open();
String query = "insert into Cart values ('" + name + "',"+ price +");";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
}
}
}
确定!当我在IIS上托管网站并在地址栏中输入查询时,它的工作原理为192 ........ / MyAndroid / Caller.aspx?Name = Banana& Price = 15 这工作正常,但从Android调用它不起作用。 这是android代码。
try {
Connectivity c = new Connectivity();
String Name = holder.txtItemName.getText().toString();
int Price = Integer.parseInt((String)holder.txtPrice.getText());
String url="http://192.168.15.2/MyAndroid/Caller.aspx?Name="+Name+"&Price="+Price; //do not use localhost
String response=c.callWebService(url);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
一点帮助将不胜感激。谢谢!!
Android清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quiz.activities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".QuizActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Add"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.Add" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Get"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.Get" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>