我在 netbeans 上构建了这个网络服务,
package in.figures.on.mobile;
import db.koneksi.dbKoneksi;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.json.simple.JSONValue;
/**
*
* @author Setyadi
*/
@WebService()
public class AksesData {
/**
* Web service operation
*/
@WebMethod(operationName = "Kategori")
public String Kategori() {
//TODO write your implementation code here:
dbKoneksi con = new dbKoneksi();
Statement statement;
Properties properties;
List list = new ArrayList();
String sql = "SELECT idPrimary_key, kategori FROM kategori ";
ResultSet hasil;
String kategori = null;
try{
statement = con.getConnection().createStatement();
hasil = statement.executeQuery(sql);
while (hasil.next()) {
properties = new Properties();
properties.put("idPrimary_key", hasil.getString(1));
properties.put("kategori", hasil.getString(2));
list.add(properties);
}
kategori = JSONValue.toJSONString(list);
}
catch(Exception e){
}
return kategori;
}
}
并返回这样的 JSON
[{"idPrimary_key":"21ye21","kategori":"FirstCategory"},
{"idPrimary_key":"89oy89","kategori":"SecondCategory"},
{"idPrimary_key":"34ew34","kategori":"ThirdCategory"}]
我尝试像这样在 Android ListView 中消费,但仍然出现错误,
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE transportSE = new HttpTransportSE(URL);
try {
transportSE.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
result = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
String jsonAN = "{\"kat\":"+result+"}"; //try to build to be like this {"kat":[{blablablaJSON}]}
String kategoriJSONList[][] = new String[99][2];
String katList[] = new String[99]; //tobe shown on listview, derived from two dimensional array above.
try {
jsonObject = new JSONObject(jsonAN);
jsonArray = jsonObject.getJSONArray("kat");
for(int i=0; i < jsonArray.length() ; i++){
kategoriJSONList[i][0] = jsonArray.getJSONObject(i).getString("idPrimary_key").toString();
kategoriJSONList[i][1] = jsonArray.getJSONObject(i).getString("kategori").toString();
}
for(int i=0; i < jsonArray.length(); i++){
katList[i] = kategoriJSONList[i][1];
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView list = (ListView) findViewById(R.id.listKategori);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
WebServiceActivity.this, android.R.layout.simple_list_item_1,katList
);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
final String kategori = (String) ((TextView)arg1).getText();
Toast.makeText(WebServiceActivity.this, kategori,
Toast.LENGTH_LONG).show();
}
});
需要帮助如何使用如上所示返回的 JSONValue 以显示为 ListView。这几天我压力很大。提前致谢。