I build this webservice on 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;
}
}
And return a JSON like this
[{"idPrimary_key":"21ye21","kategori":"FirstCategory"},
{"idPrimary_key":"89oy89","kategori":"SecondCategory"},
{"idPrimary_key":"34ew34","kategori":"ThirdCategory"}]
And I try to consume in Android ListView like this, but still got errors,
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();
}
});
Need help how to consume the JSONValue that return as shown above to be shown as ListView.
I got stress in this days.
Thanks in advance.
Ok. Try this bellow code. It is full functional to me. You should implement the HttpRequest in the commented line. Pay atention to that the JSON array is hard-coded.
And the xml files:
As result, it generates the following view:
Of course, you can customize it to create lists that you want, just parsing the jsons!
Hope that I’ve helped in some way…