Self-encapsulated functions that call WCF rest

Source: Internet
Author: User

Package www. Framework. Android;

Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. unsupportedencodingexception;
Import java. util. arraylist;
Import java. util. List;

Import org. Apache. http. header;
Import org. Apache. http. httpentity;
Import org. Apache. http. httpresponse;
Import org. Apache. http. httpstatus;
Import org. Apache. http. namevaluepair;
Import org. Apache. http. Client. clientprotocolexception;
Import org. Apache. http. Client. httpclient;
Import org. Apache. http. Client. entity. urlencodedformentity;
Import org. Apache. http. Client. Methods. httpdelete;
Import org. Apache. http. Client. Methods. httpget;
Import org. Apache. http. Client. Methods. httppost;
Import org. Apache. http. Client. Methods. httpput;
Import org. Apache. http. impl. Client. defaulthttpclient;
Import org. Apache. http. Message. basicnamevaluepair;
Import org. JSON. jsonarray;
Import org. JSON. jsonexception;
Import org. JSON. jsonobject;
// Added wcfrest processing.
Public class httprestclienthelp {


// Obtain the list, encapsulated based on httpget
Public jsonobject getrestlist (string Uri, list <namevaluepair> paramslist) throws jsonexception, ioexception
{
// Create an HTTP client
// Httpclient client = new defaulthttpclient ();
// Create a GET request

Httpget = new httpget (URI );
Httpget. setheader ("accept", "application/JSON ");
Httpget. setheader ("Content-Type", "application/JSON ");
// Send a request to the server and obtain the results returned by the server
Httpresponse response = NULL;
Try {
Response = new defaulthttpclient(cmd.exe cute (httpget );
// Response = client.exe cute (httpget );
} Catch (clientprotocolexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK)
{
System. Out. println ("OK ");
}
Else
{

String status = response. getstatusline (). getstatuscode () + "";
System. Out. println (Status );
}


// The returned results may be placed in inputstream, And the HTTP header is medium.
Inputstream = NULL;
Try {
Inputstream = response. getentity (). getcontent ();
} Catch (illegalstateexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
// Header [] headers = response. getallheaders ();
// System. Out. println (inputstream. Class. tostring ());
// System. Out. println (headers. Length + "|" + headers. tostring ());

Char [] buffer = new char [(INT) response. getentity (). getcontentlength ()];

Inputstreamreader reader = new inputstreamreader (inputstream );
Reader. Read (buffer );
Inputstream. Close ();

Jsonobject entitys = new jsonobject (new string (buffer ));
Return entitys;


}

// Obtain a single object, encapsulated based on httpget
Public jsonobject getvehicle (string URI) throws clientprotocolexception, ioexception {

// Send GET request to <service>/getvehicle/<plate>
Defaulthttpclient httpclient = new defaulthttpclient ();
Httpget request = new httpget (URI );

Request. setheader ("accept", "application/JSON ");
Request. setheader ("Content-Type", "application/JSON ");

Httpresponse response = httpclient.exe cute (request );

Httpentity responseentity = response. getentity ();

// Read response data into Buffer
Char [] buffer = new char [(INT) responseentity. getcontentlength ()];
Inputstream stream = responseentity. getcontent ();
Inputstreamreader reader = new inputstreamreader (Stream );
Reader. Read (buffer );
Stream. Close ();

Jsonobject myentity = NULL;
Try {
Myentity = new jsonobject (new string (buffer ));
} Catch (jsonexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return myentity;

// Myentity. getstring ("make ")
// Myentity. getstring ("plate ");


}


// Add an object and encapsulate the object based on httppost
Public jsonobject addentity (string Uri, list pardatalist) throws ioexception, jsonexception
{

// Create an HTTP client
Httpclient client = new defaulthttpclient ();
// Create a put request
Httppost = new httppost (URI );

Httppost. setheader ("accept", "application/JSON ");
Httppost. setheader ("Content-Type", "application/JSON ");
// Put the assembled data in httpentity and send it to the server
// Final list datalist = new arraylist ();
// Datalist. Add (New basicnamevaluepair ("price", "11.99 "));
Httpentity entity = NULL;
Try {
Entity = new urlencodedformentity (pardatalist, "UTF-8 ");
} Catch (unsupportedencodingexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Httppost. setentity (entity );

Httpresponse response = NULL;
Try {
Responseappsclient.exe cute (httppost );
} Catch (clientprotocolexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK)
{
System. Out. println ("OK ");
}
Else
{
 
String status = response. getstatusline (). getstatuscode () + "";
System. Out. println (Status );
}


// The returned results may be placed in inputstream, And the HTTP header is medium.
Inputstream = NULL;
Try {
Inputstream = response. getentity (). getcontent ();
} Catch (illegalstateexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
 

Char [] buffer = new char [(INT) response. getentity (). getcontentlength ()];

Inputstreamreader reader = new inputstreamreader (inputstream );
Reader. Read (buffer );
Inputstream. Close ();

Jsonobject entitys = new jsonobject (new string (buffer ));
Return entitys;




}

// Modify the object and encapsulate the object based on httpput
Public jsonobject updateentity (string Uri, list pardatalist) throws ioexception, jsonexception
{

// Create an HTTP client
Httpclient client = new defaulthttpclient ();
// Create a put request, such as a http://www.store.com/product/1234
Httpput = new httpput (URI );
Httpput. setheader ("accept", "application/JSON ");
Httpput. setheader ("Content-Type", "application/JSON ");
Httpentity entity = NULL;
Try {
Entity = new urlencodedformentity (pardatalist, "UTF-8 ");
} Catch (unsupportedencodingexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Httpput. setentity (entity );

Httpresponse response = NULL;
Try {
Responseappsclient.exe cute (httpput );
} Catch (clientprotocolexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK)
{
System. Out. println ("OK ");
}
Else
{

String status = response. getstatusline (). getstatuscode () + "";
System. Out. println (Status );
}


// The returned results may be placed in inputstream, And the HTTP header is medium.
Inputstream = NULL;
Try {
Inputstream = response. getentity (). getcontent ();
} Catch (illegalstateexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}


Char [] buffer = new char [(INT) response. getentity (). getcontentlength ()];

Inputstreamreader reader = new inputstreamreader (inputstream );
Reader. Read (buffer );
Inputstream. Close ();

Jsonobject entitys = new jsonobject (new string (buffer ));
Return entitys;


}

// Delete an object based on httpdelete, "http://www.store.com/product/1234"
Public jsonobject deleteentity (string URI) throws clientprotocolexception, ioexception, jsonexception
{

// Create an HTTP client
Httpclient client = new defaulthttpclient ();

// Create a Delete request
Httpdelete = new httpdelete (URI );
Httpdelete. setheader ("accept", "application/JSON ");
Httpdelete. setheader ("Content-Type", "application/JSON ");
// Send a Delete request to the server and obtain the results returned by the server. The deletion may be successful or fail.
Httpresponse response=client.exe cute (httpdelete );
If (response. getstatusline (). getstatuscode () = httpstatus. SC _ OK)
{
System. Out. println ("OK ");
}
Else
{

String status = response. getstatusline (). getstatuscode () + "";
System. Out. println (Status );
}


// The returned results may be placed in inputstream, And the HTTP header is medium.
Inputstream = NULL;
Try {
Inputstream = response. getentity (). getcontent ();
} Catch (illegalstateexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}


Char [] buffer = new char [(INT) response. getentity (). getcontentlength ()];

Inputstreamreader reader = new inputstreamreader (inputstream );
Reader. Read (buffer );
Inputstream. Close ();

Jsonobject entitys = new jsonobject (new string (buffer ));
Return entitys;

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.