Json request without parameters
InputStream is = null;
public String postData(String url)
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// response in string variable in json format
return result;
}
Json request with parameters
public void Uplode(String url1, boolean f, String guid, boolean playnow)
throws JSONException {
try {
JSONObject json = new JSONObject();
json.put("guid", guid);
json.put("description", "this video file is send by the mobile");
json.put("preempt", playnow);
json.put("front", f);
DataOutputStream wr;
String responseHeader;
HttpURLConnection connection;
URL url = new URL("http://flingo.tv/fling/fling");
// Transmit the request document
connection = (HttpURLConnection) url.openConnection();
System.out.println("url conn" + connection);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
// Send request
wr = new DataOutputStream(connection.getOutputStream());
wr.write(json.toString(1).getBytes("UTF8"));
wr.flush();
wr.close();
// Get Response
System.out.println("errroo code" + connection + "coonectiom"
+ connection.getResponseCode());
responseHeader = connection.getHeaderField(0);
if (responseHeader.contains("200")) {
System.out.println("result.........."
+ responseHeader.toString());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Response in (Json) Parsing
private void parse(String jString) throws Exception {
JSONObject jObject = new JSONObject(jString);
String attricount = jObject.getString("count");
System.out.println("count=" + attricount);
String attriyourip = jObject.getString("yourip");
System.out.println("yourip=" + attriyourip);
String attriinterval = jObject.getString("interval");
System.out.println("interval=" + attriinterval);
JSONArray deviceArray = jObject.getJSONArray("devices");
for (int i = 0; i < deviceArray.length(); i++) {
System.out.println("device_model_id="
+ deviceArray.getJSONObject(i).getString("model_id")
.toString());
device_guid_array_list.add(deviceArray.getJSONObject(i)
.getString("guid").toString());
device_guid_no = deviceArray.getJSONObject(i).getString("guid")
.toString();
JSONArray serviceArray = deviceArray.getJSONObject(i).getJSONArray(
"services");
for (int j = 0; j < serviceArray.length(); j++) {
System.out.println("service_version="
+ serviceArray.getJSONObject(j).getString("version")
.toString());
System.out.println("service_private_ip="
+ serviceArray.getJSONObject(j).getString("private_ip")
.toString());
System.out.println("service_name="
+ serviceArray.getJSONObject(j).getString("name")
.toString());
}
}
}