How to convert JSON GET request to HTTP PATCH request - java

I want to convert a HTTP GET request to a HTTP PATCH request. I am accessing TFS APIs and I want to lock my build automatically by using a patch request.
Currently I am getting all the information by GET method. Now I want to update keepForever from false to true using the HTTP PATCH method. By GET method I am able to do that but now I have to do that by HTTP Patch method.
Can someone help me converting the below code from GET method to POST method?
public class Test_URL_Req {
public static String getURLResponse(String url) {
try {
System.out.println("\nSending 'GET' request to URL : " + url);
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
// System.out.println(response);
}
in.close();
return response.toString();
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public static void main(String[] args) throws JSONException{
String url = "https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/definitions?api-version=4.1";
//String url1 ="https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&definitions=" + Def_id +"&resultFilter=succeeded&$top=1";
String response = getURLResponse(url);
// String response1 = getURLResponse(url1);
JSONObject obj_JSONObject = new JSONObject(response.toString());
JSONArray obj_JSONArray = obj_JSONObject.getJSONArray("value");
String Def_id=null;
for(int i=0; i<obj_JSONArray.length();i++)
{
JSONObject obj_JSONObject2 = obj_JSONArray.getJSONObject(i);
String value = obj_JSONObject2.getString("name");
String toSearch= "DEVOPS";
if(value.equals(toSearch)){
System.out.println("STATUS:-");
System.out.println(value);
String result =obj_JSONObject2.getString("name");
System.out.println("BUILD NAME");
System.out.println(result);
Def_id = obj_JSONObject2.get("id").toString();
System.out.println("DEFINATION ID");
System.out.println(Def_id);
break;
}
}
if (Def_id != null)
{
String url1 ="https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&definitions=" + Def_id +"&resultFilter=succeeded&$top=1";
String response1 = getURLResponse(url1);
JSONObject obj_JSONObject1 = new JSONObject(response1.toString());
JSONArray obj_JSONArray1 = obj_JSONObject1.getJSONArray("value");
String Build_id=null;
for(int i=0; i<obj_JSONArray1.length();i++)
{
JSONObject obj_JSONObject2 = obj_JSONArray1.getJSONObject(i);
String value = obj_JSONObject2.getString("result");
//String value = obj_JSONObject2.get("id").toString();
//System.out.println(value);
String toSearch1= "succeeded";
if(value.equals(toSearch1)){
System.out.println("#######################################");
System.out.println("RESULT");
System.out.println(value);
String result =obj_JSONObject2.getString("status");
System.out.println("STATUS");
System.out.println(result);
Build_id = obj_JSONObject2.get("id").toString();
System.out.println("BUILD ID");
System.out.println(Build_id);
//boolean keepForever =obj_JSONObject2.getBoolean("keepForever");
//if(keepForever == false)
//{
// keepForever=true;
//}
// System.out.println(keepForever);
}
}
if (Build_id != null)
{
String url2= "https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&buildNumber=" + Build_id;
String response2 = getURLResponse(url2);
JSONObject obj_JSONObject2 = new JSONObject(response2.toString());
JSONArray obj_JSONArray2 = obj_JSONObject2.getJSONArray("value");
for(int i=0; i<obj_JSONArray2.length();i++)
{
JSONObject obj_JSONObject3 = obj_JSONArray2.getJSONObject(i);
String value = obj_JSONObject3.getString("result");
//String value = obj_JSONObject2.get("id").toString();
//System.out.println(value);
String toSearch1= "succeeded";
if(value.equals(toSearch1)){
boolean keepForever =obj_JSONObject3.put("keepForever", false) != null;
if(keepForever == false)
{
keepForever = true;
}
System.out.println("#######################################");
System.out.println(keepForever);
}
}
}
}
}
}

You can just use the below to build PATCH request. However, you should also make sure that your server supports PATCH as its generally unsupported.
public static String getPatchResponse( String url){
try {
System.out.println("\nSending 'PATCH' request to URL : " + url);
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("PATCH");
con.setRequestProperty("Accept", "application/json");
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
//System.out.println(response);
}
in.close();
return response.toString();
} catch (Exception e) {
System.out.println(e);
}
return null;
}

Related

Java - JSONObject Not Found

I am trying to collect a single piece of data from an API, that being the population of a certain country. Everything works properly except for cutting the population value out of the JSON.
{"Info":[{"area":301336,"nativeName":"Italia","capital":"Rome","demonym":"Italian","flag":"https://restcountries.eu/data/ita.svg","alpha2Code":"IT","languages":[{"nativeName":"Italiano","iso639_2":"ita","name":"Italian","iso639_1":"it"}],"borders":["AUT","FRA","SMR","SVN","CHE","VAT"],"subregion":"Southern Europe","callingCodes":["39"],"regionalBlocs":[{"otherNames":[],"acronym":"EU","name":"European Union","otherAcronyms":[]}],"gini":36,"population":60665551,"numericCode":"380","alpha3Code":"ITA","topLevelDomain":[".it"],"timezones":["UTC+01:00"],"cioc":"ITA","translations":{"br":"Itália","de":"Italien","pt":"Itália","ja":"イタリア","hr":"Italija","it":"Italia","fa":"ایتالیا","fr":"Italie","es":"Italia","nl":"Italië"},"name":"Italy","altSpellings":["IT","Italian Republic","Repubblica italiana"],"region":"Europe","latlng":[42.83333333,12.83333333],"currencies":[{"symbol":"\u20ac","code":"EUR","name":"Euro"}]}]}
Within the JSON, It is called "Population".
This is my user input code
public static String UserInputsDetails() {
System.out.println("Please input the country name");
Scanner in = new Scanner(System.in);
String Input = in.nextLine();
return Input;
}
This is my JSON Getter Code
public static JSONArray MakeConnection(String countryname) {
JSONArray JSON = null;
try {
String url = "https://restcountries.eu/rest/v2/name/" + countryname;
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSON = new JSONArray(response.toString());
} catch (Exception e) {
System.out.println(e);
}
return JSON;
}
This is my Result code, to get just the population
public static void PrintResult(JSONArray JSON){
String population = null;
try {
JSONObject jobj = new JSONObject();
jobj.put("Info", JSON);
population = jobj.getString("population");
System.out.println(jobj);
System.out.println(population);
} catch (Exception e) {
System.out.println(e);
}
}
And finally, this is my main
public static void main(String []args) {
String Input = UserInput.UserInputsDetails();
JSONArray JSON = Connection.MakeConnection(Input);
Result.PrintResult(JSON);
}
I get the error
org.json.JSONException: JSONObject["population"] not found.
What am I doing wrong?
Delete this part:
JSONObject jobj = new JSONObject();
jobj.put("Info", JSON);
population = jobj.getString("population");
System.out.println(jobj);
System.out.println(population);
JSON is already an array, why are you converting it into a JSONObject?
Change to something like this:
Long population = JSON.getJSONObject(0).getLong("population");

How to inclosed a code in a function

This is my java code i am repeating the same steps for url and url1 so i want make a function in which i place the my url code seperate and url1 code seperate then call it in a main class. First I want to access String url and then I want to access String url1.As I am new in java so I am new in java so I am not able to enclose it into function
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONObject;
public class Test_URL_Req {
public static void main(String[] args){
// TODO Auto-generated method stub
try {
String id ="301";
String url = "https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/definitions?api-version=4.1";
String url1 ="https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&definitions=" + id +"&resultFilter=succeeded&$top=1";
URL obj = new URL(url);
URL obj1 = new URL(url1);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection();
int responseCode = con.getResponseCode();
int responseCode1 = con1.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
System.out.println("\nSending 'GET' request to URL : " + url1);
System.out.println("Response Code : " + responseCode1);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
BufferedReader in1 = new BufferedReader(
new InputStreamReader(con1.getInputStream()));
String inputLine;
String inputLine1;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
//System.out.println(response);
}
StringBuffer response1 = new StringBuffer();
while ((inputLine1 = in1.readLine()) != null) {
response1.append(inputLine1);
//System.out.println(response1);
}
in.close();
JSONObject obj_JSONObject = new JSONObject(response.toString());
JSONObject obj_JSONObject1 = new JSONObject(response1.toString());
JSONArray obj_JSONArray = obj_JSONObject.getJSONArray("value");
JSONArray obj_JSONArray1 = obj_JSONObject1.getJSONArray("value");
for(int i=0; i<obj_JSONArray.length();i++)
{
JSONObject obj_JSONObject2 = obj_JSONArray.getJSONObject(i);
String value = obj_JSONObject2.getString("name");
//String value = obj_JSONObject2.get("id").toString();
//System.out.println(value);
String toSearch= "DEVOPS";
if(value.equals(toSearch)){
System.out.println("STATUS:-");
System.out.println(value);
String result =obj_JSONObject2.getString("name");
System.out.println("BUILD NAME");
System.out.println(result);
String Def_id = obj_JSONObject2.get("id").toString();
System.out.println("DEFINATION ID");
System.out.println(Def_id);
break;
}
}
for(int i=0; i<obj_JSONArray1.length();i++)
{
JSONObject obj_JSONObject2 = obj_JSONArray1.getJSONObject(i);
String value = obj_JSONObject2.getString("result");
//String value = obj_JSONObject2.get("id").toString();
//System.out.println(value);
String toSearch1= "succeeded";
if(value.equals(toSearch1)){
System.out.println("#######################################");
System.out.println("RESULT");
System.out.println(value);
String result =obj_JSONObject2.getString("status");
System.out.println("STATUS");
System.out.println(result);
String Def_id = obj_JSONObject2.get("id").toString();
System.out.println("BUILD ID");
System.out.println(Def_id);
boolean keepForever =obj_JSONObject2.getBoolean("keepForever");
if(keepForever == false)
{
keepForever=true;
}
System.out.println(keepForever);
}
}
} catch (Exception e) {
System.out.println(e);
}
}
}
public static String getURLResponse( String url){
try {
System.out.println("\nSending 'GET' request to URL : " + url);
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
//System.out.println(response);
}
in.close();
return response.toString();
} catch (Exception e) {
System.out.println(e);
}
return null;
}
In main method -
public static void main(String[] args){
String url = "....";
String url1 =".....";
String response = getURLResponse(url);
String response1 = getURLResponse(url1);
JSONObject obj_JSONObject = new JSONObject (response);
JSONObject obj_JSONObject1 = new JSONObject(response1);
...
}
Create a method that takes a String and it appears you want a StringBuffer response...
public StringBuffer doSomething(String url){
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
int responseCode = con.getResponseCode();
//etc
return response;
}
And just pass your two URLs to it from the main:
String url = "https://tfs.tpsonline...
StringBuffer response = doSomething(url);

Appcelerator push notifications ACS REST API with java

I'm trying to send push notifications via Appcelerator rest API through my java server. I've been able to login, but when I try to send the notification I get 422 error (Unprocessable entity)
Here´s my login:
String SENDER_ID = "55694f177eead29359bda190";
String API_KEY = "bRhpzjfpHakUkYeVGbCBoFLGpqLTeKIm";
String API_USR = "tuin";
String API_PAS = "tuin123";
String URL_ACS = "https://api.cloud.appcelerator.com/v1/";
URL url = null;
URLConnection uc = null;
String idSession=null;
try {
url = new URL(URL_ACS+"users/login.json?key="+API_KEY+"&login="+API_USR+"&password="+API_PAS+"");
uc = url.openConnection();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
if (conn.getResponseCode() != 200) {
throw new Exception(conn.getResponseMessage());
}
InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
String respuesta=sb.toString();
if(respuesta.contains("status\":\"ok") && respuesta.contains("code\":200") ){
int number=sb.indexOf("session_id");
String meta=sb.substring(number+13, number+60);
int fin=meta.indexOf("\"");
idSession=meta.substring(0, fin);
}else{
System.out.println("No ID");
}
} catch (Exception e) {
throw new Exception("Trouble "+e);
}
Then I try to send the notification
public String sendPush(String date,String name, String text, String title,String session_id) throws Exception{
String URL_ACS = "https://api.cloud.appcelerator.com/v1/";
String API_KEY = "bRhpzjfpHakUkYeVGbCBoFLGpqLTeKIm";
URL url = null;
HttpURLConnection uc = null;
String idSession=null;
try {
String rt=URL_ACS+"push_notification/notify.json?key="+API_KEY+"";
url=new URL(rt);
uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type", "application/json");
uc.setRequestProperty("Accept", "application/json");
uc.setRequestProperty("Cookie","_session_id="+session_id);
JSONObject cred = new JSONObject();
JSONObject push = new JSONObject();
JSONObject chan = new JSONObject();
cred.put("alert","test");
cred.put("title","title");
cred.put("icon","icon_notifi");
cred.put("vibrate",true);
cred.put("sound","default");
push.put("payload",cred);
//chan.put("push_notification", push);
System.out.println(push.toString());
String responseJSON=push.toString().replace("{\"payload\":", "{channel=\"noti\",to_ids=\"everyone\",payload=");
OutputStreamWriter wr= new OutputStreamWriter(uc.getOutputStream());
wr.write(responseJSON);
if (uc.getResponseCode() != 200) {
throw new Exception(uc.getResponseMessage());
}
InputStream is = uc.getInputStream();
BufferedReader rd = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
uc.disconnect();
System.out.println("The content was: " + sb.toString());
} catch (Exception e) {
throw new Exception("Trouble: "+e);
}
return idSession;
}
In the second part I got 422 Error.
I got the same problem. I fixed changing to Admin: yes the user that I'm using.
http://docs.appcelerator.com/arrowdb/latest/#!/guide/admin_access

How can i get any value from json calls (Post, Get, JSON) with Selenium WebDriver on Java?

I have a following functionality: I create through the user form a new user. After i had submitted the entered data, created user get the bar-code, which would be used for get access to the other system section by scanning that bar-code with hand-scanner. So how can i get any value (in my case that bar-code from json calls (Post, Get, JSON) with Selenium WebDriver on Java?
Selenium has nothing to do with json. You can use Apache HttpClient library for sending GET, POST, PUT and DELETE requests and receiving the responses. Given below is a simplified function for all cases.
public static HttpResponse sendRequest(String requestType, String body,String url,
String... headers) throws Exception {
try {
HttpGet getRequest = null;
HttpPost postRequest;
HttpPut putRequest;
HttpDelete delRequest;
HttpResponse response = null;
HttpClient client = new DefaultHttpClient();
// Collecting Headers
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (String arg : headers) {
//Considering that you are applying header name and values in String format like this "Header1,Value1"
nvps.add(new BasicNameValuePair(arg.split(",")[0], arg
.split(",")[1]));
}
System.out.println("Total Headers Supplied " + nvps.size());
if (requestType.equalsIgnoreCase("GET")) {
getRequest = new HttpGet(url);
for (NameValuePair h : nvps) {
getRequest.addHeader(h.getName(), h.getValue());
}
response = client.execute(getRequest);
}
if (requestType.equalsIgnoreCase("POST")) {
postRequest = new HttpPost(url);
for (NameValuePair h : nvps) {
postRequest.addHeader(h.getName(), h.getValue());
}
StringEntity requestEntity = new StringEntity(body,"UTF-8");
postRequest.setEntity(requestEntity);
response = client.execute(postRequest);
}
if (requestType.equalsIgnoreCase("PUT")) {
putRequest = new HttpPut(url);
for (NameValuePair h : nvps) {
putRequest.addHeader(h.getName(), h.getValue());
}
StringEntity requestEntity = new StringEntity(body,"UTF-8");
putRequest.setEntity(requestEntity);
response = client.execute(putRequest);
}
if (requestType.equalsIgnoreCase("DELETE")) {
delRequest = new HttpDelete(url);
for (NameValuePair h : nvps) {
delRequest.addHeader(h.getName(), h.getValue());
}
response = client.execute(delRequest);
}
return response;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
Selenium only deals with browsers.
Java has classes that do http requests.
see the code below:
private HttpURLConnection setODataConnection(String url, String method) {
try {
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setRequestMethod(method);
// add request header
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json;odata=verbose");
return conn;
} catch (Exception e) {
Assert.fail(e.getMessage());
return null;
}
}
private StringBuilder sendODataRequest(HttpURLConnection conn) {
try {
int responseCode = conn.getResponseCode();
String method = conn.getRequestMethod();
System.out.println("\nSending '" + method + "' request to URL : " + conn.getURL());
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response;
} catch (Exception e) {
Assert.fail(e.getMessage());
return null;
}
}
private ArrayList<String> getByFullUrl(String fullUrl, String entity) {
HttpURLConnection conn = setODataConnection(fullUrl, "GET");
StringBuilder response = sendODataRequest(conn);
ArrayList<String> s = new ArrayList<String>();
Pattern p = Pattern.compile(entity + "\" : (.*?)\\}");
Matcher m = p.matcher(response);
while (m.find()) {
s.add(m.group(1).replace("\"", ""));
}
return s;
}
public ArrayList<String> get(String table, String entity) {
String url = oDataUrl + table + "?$select=" + entity;
return getByFullUrl(url, entity);
}
public void post(String table, String bodyDetails) {
String url = oDataUrl + table;
HttpURLConnection conn = setODataConnection(url, "POST");
conn.setDoOutput(true);
try {
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes("details={" + bodyDetails + "}");
wr.flush();
wr.close();
} catch (Exception e) {
Assert.fail(e.getMessage());
}
sendODataRequest(conn);
}
public void put(String table, String id, String bodyDetails) {
String url = oDataUrl + table + "(" + id + ")";
HttpURLConnection conn = setODataConnection(url, "PUT");
conn.setDoOutput(true);
try {
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes("details={" + bodyDetails + "}");
wr.flush();
wr.close();
} catch (Exception e) {
Assert.fail(e.getMessage());
}
sendODataRequest(conn);
}

Java: How to read content from redirected URLs?

I use the following Java code in a Bean to read a URL's content:
String url;
String inputLine;
StringBuilder srcCode=new StringBuilder();
public void setUrl (String value) {
url = value;
}
private void scanWebPage() throws IOException {
try {
URL dest = new URL(url);
URLConnection yc = dest.openConnection();
yc.setUseCaches(false);
BufferedReader in = new BufferedReader(new
InputStreamReader(yc.getInputStream()));
while ((inputLine = in.readLine()) != null)
srcCode = srcCode.append (inputLine);
in.close();
} catch (FileNotFoundException fne) {
srcCode.append("File Not Found") ;
}
}
The code works fine for most URL's, but does not work for redirected URLs. How can I update the above code to read content from redirected URLs? For redirected URLs, I get "File Not Found".
Give the following a go:
HttpURLConnection yc = (HttpURLConnection) dest.openConnection();
yc.setInstanceFollowRedirects( true );
In context to your code above:
`String url = "http://java.sun.com";
String inputLine;
StringBuilder srcCode=new StringBuilder();
URL dest = new URL(url);
HttpURLConnection yc = (HttpURLConnection) dest.openConnection();
yc.setInstanceFollowRedirects( true );
yc.setUseCaches(false);
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
while ((inputLine = in.readLine()) != null) {
srcCode = srcCode.append (inputLine);
}
in.close();`
Modified further to help you diagnose what is going on. This code turns off auto redirection and then manually follows the Location headers printing out as it goes along.
#Test
public void f() throws IOException {
String url = "http://java.sun.com";
fetchURL(url);
}
private HttpURLConnection fetchURL( String url ) throws IOException {
URL dest = new URL(url);
HttpURLConnection yc = (HttpURLConnection) dest.openConnection();
yc.setInstanceFollowRedirects( false );
yc.setUseCaches(false);
System.out.println( "url = " + url );
int responseCode = yc.getResponseCode();
if ( responseCode >= 300 && responseCode < 400 ) { // brute force check, far too wide
return fetchURL( yc.getHeaderField( "Location") );
}
System.out.println( "yc.getResponseCode() = " + yc.getResponseCode() );
return yc;
}
its not the debuggin of your prog , but you can consider this one
public class GetURLData
{
public static void main(String args[])
{
String url = "the url you want the response from";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response;
StringBuilder builder= new StringBuilder();
try
{
response = httpClient.execute(httpPost);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
char[] buf = new char[8000];
int l = 0;
while (l >= 0)
{
builder.append(buf, 0, l);
l = in.read(buf);
}
System.out.println(builder.toString);
} catch (Exception e)
{
System.out.println("Exception is :"+e);
e.printStackTrace();
}
}
}

Categories

Resources