我将我的WordPress网站的MySQL数据库与C#WebService连接起来。
当我在LocalHost中运行Webservice从我的网站返回值时,但是当我发布webservice并且我使用Android应用程序发送值时返回“null”。
当我更改WebService以返回“Hello World”Android应用程序返回“Hello World”时。
我该如何解决这个问题?
WebService的:
[WebMethod]
public string GetYazilar(int KategoriID)
{
string YaziID = "";
MySqlConnection baglanti = new MySqlConnection("Server=xx.xx.xx.xxx; Database=wordpress_e;Uid=xx;Pwd='xxx';");
baglanti.Open();
DataSet ds = null;
//MySqlCommand cmd = new MySqlCommand("SELECT * FROM 1sbwt_term_taxonomy INNER JOIN 1sbwt_term_relationships ON 1sbwt_term_relationships.term_taxonomy_id = 1sbwt_term_taxonomy.term_id INNER JOIN 1sbwt_posts ON 1sbwt_term_relationships.object_id = 1sbwt_posts.ID WHERE 1sbwt_term_taxonomy.term_id = " + KategoriID + " ");
MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM 3tkqs_term_taxonomy INNER JOIN 3tkqs_term_relationships ON 3tkqs_term_relationships.term_taxonomy_id = 3tkqs_term_taxonomy.term_id INNER JOIN 3tkqs_posts ON 3tkqs_term_relationships.object_id = 3tkqs_posts.ID WHERE 3tkqs_term_taxonomy.term_id = " + KategoriID + "", baglanti);
ds = new DataSet();
da.Fill(ds);
if (ds != null && ds.Tables != null && ds.Tables.Count > 0 &&
ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
{
DataTable tbl = ds.Tables[0];
for (int i = 0; i < tbl.Rows.Count; i++)
{
YaziID = tbl.Rows[i]["ID"].ToString();
}
}
return YaziID;
}
Android应用程序:
public class MainActivity extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://swift-programming.net/WebService1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/GetYazilar";
private final String METHOD_NAME = "GetYazilar";
private static String input;
private static String Result;
Button b;
TextView tv;
EditText et;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.editText1);
tv = (TextView) findViewById(R.id.tv_result);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (et.getText().length() != 0 && et.getText().toString() != "") {
input = et.getText().toString();
AsyncCallWS task = new AsyncCallWS();
task.execute();
} else {
tv.setText("Please Enter Input");
}
}
});
}
public void getFahrenheit(String input) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsiusPI = new PropertyInfo();
celsiusPI.setName("Input");
celsiusPI.setValue(input);
celsiusPI.setType(double.class);
request.addProperty(celsiusPI);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
Result = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
getFahrenheit(input);
return null;
}
@Override
protected void onPostExecute(Void result) {
tv.setText(Result + " :");
}
@Override
protected void onPreExecute() {
tv.setText("Loading...");
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
}