รบกวนสอบถามหน่อยครับ
พอดีว่าผมต้องการให้มีการส่งค่าจากตัวแอพ ไปยัง service และให้ service เข้า case ว่าตรงกับ case ไหนตามไฟล์ WebServiceServer.php และทำการส่งข้อมูลเป็น Array Jason กลับไปยังตัวแอพ และให้ตัวแอพโชว์ข้อมูล โดยการประยุกต์ ตาม https://www.thaicreate.com/mobile/android-topic-json.html อะครับ พอลองรันดูแล้วส่งข้อมูลตาม case ที่ตั้งไว้ กลับไม่มีอะไรเกิดขึ้น เลยมาขอความช่วยเหลืออะครับ ขอบคุณครับ
WebServiceServer.php
<?
require_once("lib/nusoap.php");
//Create a new soap server
$server = new soap_server();
//Define our namespace
$namespace = "http://localhost/nusoap/WebServiceServer.php";
$server->wsdl->schemaTargetNamespace = $namespace;
//Configure our WSDL
$server->configureWSDL("SPUguide");
// Register our method and argument parameters
$varname = array(
'idAction' => "xsd:string"
);
$server->register('HelloWorld',$varname, array('return' => 'xsd:string'));
function HelloWorld($idAction)
{
$hostname = "localhost"; //ชื่อโฮสต์
$user = "root"; //ชื่อผู้ใช้
$password = ""; //รหัสผ่าน
$dbname = "test"; //ชื่อฐานข้อมูล
// เริ่มติดต่อฐานข้อมูล
$dbCon=mysql_connect($hostname, $user, $password) or die("ติดต่อฐานข้อมูลไม่ได้");
// เลือกฐานข้อมูล
mysql_select_db($dbname,$dbCon) or die("เลือกฐานข้อมูลไม่ได้");
mysql_query('SET CHARACTER SET UTF8');
switch($idAction){
case 'sub':
$strSQL = "SELECT * FROM subject";
$result = mysql_query($strSQL);
$intNumField = mysql_num_fields($result);
$resultArray = array();
while($obResult = mysql_fetch_array($result)){
$arrCol = array();
for($i=0;$i<$intNumField;$i++){
$arrCol[mysql_field_name($result,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
mysql_close($objConnect);
return json_encode($resultArray);
break;
}
}
// Get our posted data if the service is being consumed
// otherwise leave this data blank.
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();
?>
MainActivity.java
package com.andriod.testservice;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private final String NAMESPACE = "http://10.0.2.2/nusoap/WebServiceServer.php";
private final String URL = "http://10.0.2.2/nusoap/WebServiceServer.php?wsdl"; // WSDL URL
private final String SOAP_ACTION = "http://10.0.2.2/nusoap/WebServiceServer.php/HelloWorld";
private final String METHOD_NAME = "HelloWorld"; // Method on web service
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String,String>>();
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
// btnSend
Button btnSend = (Button) this.findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText txtName = (EditText) findViewById(R.id.txtName);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("idAction",txtName.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
JSONArray data = new JSONArray(result.toString());
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("SubjectID", c.getString("ID_Sub"));
map.put("SubjectName", c.getString("Name_Sub"));
MyArrList.add(map);
}
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.activity_column,
new String[] {"SubjectID", "SubjectName"}, new int[] {R.id.ColSubID,
R.id.ColSubName});
lisView1.setAdapter(sAdap);
} catch (IOException e) {
e.printStackTrace();
}catch (XmlPullParserException e) {
e.printStackTrace();
}catch (JSONException e) {
e.printStackTrace();
}
}
});
final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);
lisView1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> myAdapter, View myView,int position, long mylng) {
String sMemberID = MyArrList.get(position).get("SubjectID").toString();
String sName = MyArrList.get(position).get("SubjectName").toString();
viewDetail.setIcon(android.R.drawable.btn_star_big_on);
viewDetail.setTitle("Subject Detail");
viewDetail.setMessage("SubjectID : " + sMemberID + "\n"+ "SubjectName : " + sName + "\n");
viewDetail.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
viewDetail.show();
};
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}