summaryrefslogtreecommitdiffstats
path: root/sandbox/travelsample/contrib/scatours-android-ui/src
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/travelsample/contrib/scatours-android-ui/src')
-rw-r--r--sandbox/travelsample/contrib/scatours-android-ui/src/scatours/SCAToursSearch.java29
-rw-r--r--sandbox/travelsample/contrib/scatours-android-ui/src/scatours/SCAToursSearchProxy.java81
-rw-r--r--sandbox/travelsample/contrib/scatours-android-ui/src/scatours/android/TripSearch.java186
-rw-r--r--sandbox/travelsample/contrib/scatours-android-ui/src/scatours/android/TripSearchResults.java57
-rw-r--r--sandbox/travelsample/contrib/scatours-android-ui/src/scatours/common/TripItem.java219
-rw-r--r--sandbox/travelsample/contrib/scatours-android-ui/src/scatours/common/TripLeg.java97
-rw-r--r--sandbox/travelsample/contrib/scatours-android-ui/src/scatours/jsonrpc/JSONRpc.java63
7 files changed, 732 insertions, 0 deletions
diff --git a/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/SCAToursSearch.java b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/SCAToursSearch.java
new file mode 100644
index 0000000000..36a514b0b8
--- /dev/null
+++ b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/SCAToursSearch.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package scatours;
+
+import scatours.common.TripItem;
+import scatours.common.TripLeg;
+
+/**
+ * The Trip service interface
+ */
+public interface SCAToursSearch {
+ TripItem[] search(TripLeg tripLeg);
+}
diff --git a/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/SCAToursSearchProxy.java b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/SCAToursSearchProxy.java
new file mode 100644
index 0000000000..cfd6b98bcf
--- /dev/null
+++ b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/SCAToursSearchProxy.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import scatours.common.TripItem;
+import scatours.common.TripLeg;
+import scatours.jsonrpc.JSONRpc;
+
+public class SCAToursSearchProxy implements SCAToursSearch {
+ // see http://developer.android.com/guide/developing/tools/emulator.html
+ private static final String jsonRPCServiceURI = "http://10.0.2.2:8080/SCAToursComponent/SCAToursSearch";
+ private static final String jsonRPCRequest = "{\"id\": 5, \"method\": \"Service.search\", \"params\": [{\"id\": \"5f9a10f2-527f-4d91-a13c-b1aa2baaedd8\", \"fromLocation\": \"LGW\", \"toLocation\": \"FLR\", \"fromDate\": \"06/12/09\", \"toDate\": \"13/12/09\", \"noOfPeople\": \"2\"}]}";
+
+ private List<TripItem> tripCatalog = new ArrayList<TripItem>();
+
+ public SCAToursSearchProxy() {
+ initialize();
+ }
+
+ public void initialize() {
+ JSONObject json = null;
+
+ try {
+ json = JSONRpc.invoke(jsonRPCServiceURI, jsonRPCRequest);
+
+ if(json == null) {
+ return;
+ }
+
+ JSONArray result = json.getJSONArray("result");
+ for(int i = 0; i < result.length(); i++) {
+ TripItem item = new TripItem();
+
+ item.setType(result.getJSONObject(i).getString("type"));
+ item.setName(result.getJSONObject(i).getString("name"));
+ item.setDescription(result.getJSONObject(i).getString("description"));
+ item.setLocation(result.getJSONObject(i).getString("location"));
+ item.setFromDate(result.getJSONObject(i).getString("fromDate"));
+ item.setToDate(result.getJSONObject(i).getString("toDate"));
+ item.setPrice(result.getJSONObject(i).getDouble("price"));
+ item.setCurrency(result.getJSONObject(i).getString("currency"));
+
+ tripCatalog.add(item);
+ }
+
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ public TripItem[] search(TripLeg tripLeg) {
+ TripItem[] catalogArray = new TripItem[tripCatalog.size()];
+ tripCatalog.toArray(catalogArray);
+ return catalogArray;
+ }
+}
diff --git a/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/android/TripSearch.java b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/android/TripSearch.java
new file mode 100644
index 0000000000..b64c14a583
--- /dev/null
+++ b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/android/TripSearch.java
@@ -0,0 +1,186 @@
+package scatours.android;
+
+import scatours.SCAToursSearch;
+import scatours.SCAToursSearchProxy;
+import scatours.common.TripItem;
+import scatours.common.TripLeg;
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.AutoCompleteTextView;
+import android.widget.Button;
+import android.widget.AdapterView.OnItemClickListener;
+import android.widget.AdapterView.OnItemSelectedListener;
+
+public class TripSearch extends Activity {
+
+ static final String[] AIRPORT_CODES = new String[] {
+ "LGW - London Gatwick Airport",
+ "FLR - Luigi Ridolfi Airport",
+ "SFO - San Francisco Airport",
+ "GRU - Sao Paulo Airport",
+ "GIG - Rio de Janeiro Airport"
+ };
+
+
+ private AutoCompleteTextView txtFromLocation,
+ txtToLocation,
+ txtDateStart,
+ txtDateEnd,
+ txtNumberOfPeople;
+ private Button btnSearch;
+
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.search);
+
+
+ ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, AIRPORT_CODES);
+
+ txtFromLocation = (AutoCompleteTextView) findViewById(R.id.edit_fromLocation);
+ //ArrayAdapter adapterFrom = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, AIRPORT_CODES);
+ txtFromLocation.setAdapter(adapter);
+
+ txtToLocation = (AutoCompleteTextView) findViewById(R.id.edit_toLocation);
+ //ArrayAdapter adapterTo = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line, AIRPORT_CODES);
+ txtToLocation.setAdapter(adapter);
+
+ txtDateStart = (AutoCompleteTextView) findViewById(R.id.edit_date_start);
+ txtDateEnd = (AutoCompleteTextView) findViewById(R.id.edit_date_end);
+ txtNumberOfPeople = (AutoCompleteTextView) findViewById(R.id.edit_NumberOfPeople);
+
+ btnSearch = (Button) this.findViewById(R.id.btnSearch);
+
+ doListen();
+ }
+
+ private void doListen() {
+
+ txtFromLocation.setOnItemClickListener(new OnItemClickListener() {
+ public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+ Log.i(getString(R.string.app_name),">>Item Clicked: " +AIRPORT_CODES[position]);
+ }
+
+ });
+
+ txtFromLocation.setOnItemSelectedListener(new OnItemSelectedListener() {
+
+ public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
+ Log.i(getString(R.string.app_name),">>Item Selected: " +AIRPORT_CODES[position]);
+ }
+
+ public void onNothingSelected(AdapterView<?> parent) {
+ Log.i(getString(R.string.app_name),">>Selection cleared");
+ }
+
+ });
+
+
+ //closeButton
+ btnSearch.setOnClickListener( new OnClickListener() {
+ public void onClick(View v) {
+
+ doTripSearch(v);
+ /*
+ //Search trips here
+ new AlertDialog.Builder(TripSearch.this)
+ .setTitle("SCATour")
+ .setMessage("You're about to search for trips !")
+ .setIcon(R.drawable.icon)
+ .setPositiveButton(R.string.alert_yes, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int whichButton) {
+
+ }})
+ .setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener(){
+ public void onClick(DialogInterface dialog, int which) {
+
+ }
+ })
+ .show();*/
+ }
+ });
+
+ }
+
+
+ private void doTripSearch(View view) {
+ SCAToursSearch searchProxy = new SCAToursSearchProxy();
+ TripLeg tripLeg = new TripLeg();
+
+ Log.i(getString(R.string.app_name),"From : " + txtFromLocation.getText().toString() );
+ Log.i(getString(R.string.app_name),"To : " + txtToLocation.getText().toString() );
+ Log.i(getString(R.string.app_name),"Start Date : " + txtDateStart.getText().toString() );
+ Log.i(getString(R.string.app_name),"End Date : " + txtDateEnd.getText().toString() );
+ Log.i(getString(R.string.app_name),"NumberOfPeople : " + txtNumberOfPeople.getText().toString() );
+
+ tripLeg.setFromLocation("LGW");
+ tripLeg.setToLocation("FLR");
+ tripLeg.setFromDate("06/12/09");
+ tripLeg.setToDate("13/12/09");
+ tripLeg.setNoOfPeople("2");
+
+ TripItem[] tripsAvailable = searchProxy.search(tripLeg);
+ Log.i(getString(R.string.app_name),"Found " + tripsAvailable.length + " trips");
+
+ Log.i(getString(R.string.app_name),"Calling Results view...");
+
+ displayTripSearchResults(tripsAvailable);
+
+ /*
+ try {
+ Intent resultView = new Intent(this, TripSearchResults.class);
+ resultView.putExtra("results", tripsAvailable);
+ startActivity(resultView);
+ } catch(Exception e) {
+ Log.e(getString(R.string.app_name), e.getMessage());
+ }
+ */
+
+ //TripSearchResults searchResults = new TripSearchResults(tripsAvailable);
+ //searchResults.setContentView(R.layout.search_results);
+
+ Log.i(getString(R.string.app_name),"Called...");
+
+ }
+
+ private void displayTripSearchResults(TripItem[] tripsAvailable) {
+
+ String result = "";
+ for(TripItem item : tripsAvailable) {
+ Log.i(getString(R.string.app_name), "Item type:" + item.getType());
+ if (item.getType().equals(TripItem.TRIP)) {
+ result += item.getDescription() + " (" + item.getCurrency() + " " + item.getPrice() + ")\n";
+ }
+ }
+
+
+ //Search trips here
+ new AlertDialog.Builder(TripSearch.this)
+ .setTitle("SCATour")
+ .setMessage("Found:\n" + result)
+ .setIcon(R.drawable.icon)
+ .setCancelable(false)
+ .setPositiveButton(R.string.alert_yes, new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int whichButton) {
+
+ }})
+ .setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener(){
+ public void onClick(DialogInterface dialog, int which) {
+
+ }
+ })
+ .show();
+
+ }
+
+} \ No newline at end of file
diff --git a/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/android/TripSearchResults.java b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/android/TripSearchResults.java
new file mode 100644
index 0000000000..002ef8def1
--- /dev/null
+++ b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/android/TripSearchResults.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.android;
+
+import scatours.common.TripItem;
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+import android.widget.ArrayAdapter;
+import android.widget.ListView;
+
+public class TripSearchResults extends Activity {
+ private TripItem[] results;
+
+ private ListView listResults;
+
+ public TripSearchResults( ) {
+ super();
+ }
+
+ public void setResults(TripItem[] results) {
+ Log.i(getString(R.string.app_name),"Setting results : " + results.length);
+ this.results = results;
+ }
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.search_results);
+
+ Log.i(getString(R.string.app_name),"Displaying " + results.length + " trips");
+
+ listResults = (ListView) findViewById(R.id.listPackages);
+ //listResults.setAdapter(new ArrayAdapter<TripItem>(this, R.id.listPackages, results));
+
+
+ }
+
+}
diff --git a/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/common/TripItem.java b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/common/TripItem.java
new file mode 100644
index 0000000000..1fd78c8dd5
--- /dev/null
+++ b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/common/TripItem.java
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.common;
+
+
+
+public class TripItem {
+
+ public static String FLIGHT = "Flight";
+ public static String HOTEL = "Hotel";
+ public static String CAR = "Car";
+ public static String TRIP = "Trip";
+
+ private String id;
+ private String tripId;
+ private String type;
+ private String name;
+ private String description;
+ private String location;
+ private String fromDate;
+ private String toDate;
+ private double price;
+ private String currency;
+ private String link;
+ private TripItem[] tripItems; // used for a trip made up of trip items
+ private String customerDetails;
+ private String agentDetails;
+ private String bookingCode;
+
+ public TripItem() {
+ }
+
+ public TripItem(TripItem item) {
+ this.id = item.getId();
+ this.tripId = item.getTripId();
+ this.type = item.getType();
+ this.name = item.getName();
+ this.description = item.getDescription();
+ this.location = item.getLocation();
+ this.fromDate = item.getFromDate();
+ this.toDate = item.getToDate();
+ this.price = item.getPrice();
+ this.currency = item.getCurrency();
+ this.link = item.getLink();
+ }
+
+ public TripItem(String id,
+ String tripId,
+ String type,
+ String name,
+ String description,
+ String location,
+ String fromDate,
+ String toDate,
+ double price,
+ String currency,
+ String link) {
+ this.id = id;
+ this.tripId = tripId;
+ this.type = type;
+ this.name = name;
+ this.description = description;
+ this.location = location;
+ this.fromDate = fromDate;
+ this.toDate = toDate;
+ this.price = price;
+ this.currency = currency;
+ this.link = link;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getTripId() {
+ return tripId;
+ }
+
+ public void setTripId(String tripId) {
+ this.tripId = tripId;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String type) {
+ this.type = type;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+ public String getLocation() {
+ return location;
+ }
+
+ public void setLocation(String location) {
+ this.location = location;
+ }
+
+ public String getFromDate() {
+ return fromDate;
+ }
+
+ public void setFromDate(String fromDate) {
+ this.fromDate = fromDate;
+ }
+
+ public String getToDate() {
+ return toDate;
+ }
+
+ public void setToDate(String toDate) {
+ this.toDate = toDate;
+ }
+
+ public double getPrice() {
+ return price;
+ }
+
+ public void setPrice(double price) {
+ this.price = price;
+ }
+
+ public String getCurrency() {
+ return currency;
+ }
+
+ public void setCurrency(String currency) {
+ this.currency = currency;
+ }
+
+ public String getLink() {
+ return link;
+ }
+
+ public void setLink(String link) {
+ this.link = link;
+ }
+
+ public TripItem[] getTripItems() {
+ return tripItems;
+ }
+
+ public void setTripItems(TripItem[] tripItems) {
+ this.tripItems = tripItems;
+ }
+
+ public String getCustomerDetails() {
+ return customerDetails;
+ }
+
+ public void setCustomerDetails(String customerDetails) {
+ this.customerDetails = customerDetails;
+ }
+
+ public String getAgentDetails() {
+ return agentDetails;
+ }
+
+ public void setAgentDetails(String agentDetails) {
+ this.agentDetails = agentDetails;
+ }
+
+ public String getBookingCode() {
+ return bookingCode;
+ }
+
+ public void setBookingCode(String bookingCode) {
+ this.bookingCode = bookingCode;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+
+ if (obj instanceof TripItem){
+ if (((TripItem)obj).getId().equals(getId())){
+ return true;
+ }
+ }
+
+ return super.equals(obj);
+ }
+}
diff --git a/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/common/TripLeg.java b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/common/TripLeg.java
new file mode 100644
index 0000000000..1af33aa6f9
--- /dev/null
+++ b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/common/TripLeg.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.common;
+
+
+public class TripLeg {
+
+ private String id;
+ private String fromLocation;
+ private String toLocation;
+ private String fromDate;
+ private String toDate;
+ private String noOfPeople;
+
+
+ public TripLeg() {
+ }
+
+ public TripLeg(String id,
+ String fromLocation,
+ String toLocation,
+ String fromDate,
+ String toDate,
+ String noOfPeople) {
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getFromLocation() {
+ return fromLocation;
+ }
+
+ public void setFromLocation(String fromLocation) {
+ this.fromLocation = fromLocation;
+ }
+
+ public String getToLocation() {
+ return toLocation;
+ }
+
+ public void setToLocation(String toLocation) {
+ this.toLocation = toLocation;
+ }
+
+ public String getFromDate() {
+ return fromDate;
+ }
+
+ public void setFromDate(String fromDate) {
+ this.fromDate = fromDate;
+ }
+
+ public String getToDate() {
+ return toDate;
+ }
+
+ public void setToDate(String toDate) {
+ this.toDate = toDate;
+ }
+
+ public String getNoOfPeople() {
+ return noOfPeople;
+ }
+
+ public void setNoOfPeople(String noOfPeople) {
+ this.noOfPeople = noOfPeople;
+ }
+
+
+
+
+
+
+}
diff --git a/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/jsonrpc/JSONRpc.java b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/jsonrpc/JSONRpc.java
new file mode 100644
index 0000000000..c6a9cd8702
--- /dev/null
+++ b/sandbox/travelsample/contrib/scatours-android-ui/src/scatours/jsonrpc/JSONRpc.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package scatours.jsonrpc;
+
+import java.io.IOException;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class JSONRpc {
+
+ protected JSONRpc() {
+
+ }
+
+ public static JSONObject invoke(String serviceURI, String rpcRequest) throws JSONException{
+ HttpClient httpClient = new DefaultHttpClient();
+ HttpPost httpPost = new HttpPost(serviceURI);
+
+ JSONObject result = null;
+ try {
+ httpPost.setHeader("Content-Type", "text/xml");
+ httpPost.setEntity(new StringEntity(rpcRequest));
+
+ HttpResponse httpResponse = httpClient.execute(httpPost);
+ if (httpResponse.getStatusLine().getStatusCode() == 200) {
+ String jsonResult = EntityUtils.toString(httpResponse.getEntity());
+ result = new JSONObject(jsonResult);
+ } else {
+ String errorMessage = httpResponse.getStatusLine()
+ .getReasonPhrase();
+ System.out.println(errorMessage);
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return result;
+ }
+}