{"id":20069350,"url":"https://github.com/telamon/android-ajax.request","last_synced_at":"2025-03-02T11:24:00.814Z","repository":{"id":73330635,"uuid":"1725814","full_name":"telamon/Android-Ajax.Request","owner":"telamon","description":"REST helper  for Android, inspired by prototypejs' Ajax.Request","archived":false,"fork":false,"pushed_at":"2011-07-25T21:14:55.000Z","size":158,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-12T23:33:08.285Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/telamon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-05-10T01:56:08.000Z","updated_at":"2017-12-03T14:08:38.000Z","dependencies_parsed_at":"2023-02-22T05:25:26.172Z","dependency_job_id":null,"html_url":"https://github.com/telamon/Android-Ajax.Request","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telamon%2FAndroid-Ajax.Request","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telamon%2FAndroid-Ajax.Request/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telamon%2FAndroid-Ajax.Request/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telamon%2FAndroid-Ajax.Request/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/telamon","download_url":"https://codeload.github.com/telamon/Android-Ajax.Request/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241495639,"owners_count":19972137,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T14:13:47.602Z","updated_at":"2025-03-02T11:24:00.793Z","avatar_url":"https://github.com/telamon.png","language":"Java","readme":"Ajax.Request for Android\n========================\nA sweet way of writing your everyday http requests.\n\n\nI'm used to writing webservice requests in JavaScript and when I arrived\nto the android platform I realized that the HttpClient-request-way left much\nto be desired compared to prototypejs' Ajax.Request with auto-serialization, content handling and\ngeneral lazyness.\n\nFeatures\n---------\n* Automatic content handling for XML and JSON\n* Built on top of AsyncTask to ensure that your callback code runs on the UI thread.\n* Provides general lazyness.\n* Failsafe? (No guarantees yet)\n* Better resource handling to be able to receive binary data responses?\n### TODO\n\n* JSON Header support\n* More ways to handle XML post data?\n* Rails authenticity token handling for \"application/x-www-form-urlencoded\" postdata\n\nRequesting Things (The Request class)\n-------------------------------------\n\n\n### Example usage\nInclude the library in whatever fashion you desire\n\n#### Simple GET request\n\n\tRequest r = new Request(\"http://mydomain.org/service/service.json\"){\n\t\t\n\t\t// Optional callback override.\n\t\t@Override\n\t\tprotected void onSuccess(Transport transport) {\n\t\t\t// Your handling code goes here,\n\t\t\t// The 'transport' object holds all the desired response data.\n\t\t\t\t\t\t\n\t\t\tEditText et = (EditText) this.findViewById(R.id.editText1);\n\t\t\tet.getText().append( transport.getResponseJson().optString(\"foo\") );\n\t\t\t\t\t\t\n\t\t}\t\t\t\t\t\n\t}.execute(\"GET\");\t\n\t\t\t\t\t\n#### Simple POST request\n\n\tContext context = getApplicationContext();\t\n\tstartMyProgressIndicator();\n\t\n\tRequest r = new Request(\"http://mydomain.org/service/highscore\"){\n\t\t\n\t\t// Optional callback override.\n\t\t@Override\n\t\tprotected void onSuccess(Transport transport) {\n\t\t\t// Grab the desired nodes with the xpath helper\t\t\t\n\t\t\torg.w3c.dom.NodeList users = transport.findByXpath('//users');\n\t\t\t\n\t\t\t// do something.\n\t\t\tfor(org.w3c.dom.Node user:users){\n\t\t\t\taddUserToMyList(user);\n\t\t\t}\n\t\t}\n\t\t\n\t\t\n\t\t// Optional callback override.\n\t\t@Override\n\t\tprotected void onComplete(Transport transport) {\n\t\t\tstopMyProgressIndicator();\n\t\t}\n\t\t\n\t\t\n\t\t// Optional callback override.\n\t\t@Override\n\t\tprotected void onError(IOException ex) {\n\t\t\tToast.makeText(context, \"Error occured: \" + ex.getMessage(), Toast.LENGTH_SHORT);\n\t\t}\n\t\t\n\t\t\n\t\t// Optional callback override.\n\t\t@Override\n\t\tprotected void onFailure(Transport transport) {\n\t\t\tToast.makeText(context, \"Something went wrong. code: \" + transport.getStatus(),Toast.LENGTH_SHORT);\t\t\t\t\t\t\t\t\n\t\t}\n\t\t\t\n\t\t\n\t}\n\t\n\t// Tell the webserver that we would like to have the response in XML\n\tr.accept(Request.CTYPE_XML); \n\t\n\t// Tell the webserver that our post data is in JSON\n\tr.setContentType(Request.CTYPE_JSON);\n\t\t\n\tr.execute(\"POST\",\"{ \\\"name\\\" : \\\"MrMan\\\", \\\"score\\\" : 9001 }\");\n\t\n\t\n### Headers\nYou can set whatever HTTPHeader you like but they must be set _before_ the execute() call.\n\n\trequest.setHeader(\"If-Modified-Since\", \"Tue, 10 May 2011 11:31:56 GMT\")\n\nThere are a few shortcuts like\n\n\trequest.accept(\"text/plain\"); // Modifies the 'Accept' header\n\tsetContentType(\"application/json\");\t // Modifies the 'Content-Type' header\n\n### Parameters\n\nYou can easily set POST or PUT data by calling\n\n\tr.setParams(myBadAssObject)\n\nor provide your data as the second parameter to execute, eg: \n\n\tr.execute(\"POST\",myKewlObject)\n\nsetParams(Object) method tries to automatically determine the type of content you're\ntrying to send, and automatically sets the Content-Type header accordingly.\n\nThe detected data is then delegated to one of the following methods:\n\n#### Urlencoded form \n\n\tsetFormParams(new String[][]{\n\t\t{\"foo\",\"bar\"},\n\t\t{\"eat\",\"bacon\"}\n\t});\n\t\nConverts your data to an UrlEncodedFormEntity and calls setContent(CTYPE_FORM).\n\t\n\tsetFormParams(HashMap\u003c?, ?\u003e)\nDoes the same as above except it takes an hashmap as input and ultimately calls\ntoString() on each key / value.\n\n#### JSON\n\n\tsetJsonParams(org.json.JSONObject)\nSets Content-Type header to application/json.\nConverts your JSONObject to a String and then delegates your data to setStringParams()\n\n#### XML\n\t \t\n\tsetXmlParams(org.w3c.dom.Document)\nSets Content-Type header to application/xml,\nConverts your Xml Document to a String and then delegates your data to setStringParams()\n\n#### Plain text\n\t\n\tsetStringParams(String)\nCreates a StringEntity from your data and sets it to be sent with the request.\n\t\n#### note\nAs of this moment there's no sophisticated string content type detection.\nThe only case that setStringParams() modifies the \"Content-Type\" header\nis when the string begins with __'\u003c?xml'__.\n\nYes this might seem like a design flaw and might trigger the \"setParams calls modified\nmy Content-Type header when I've already set it manually\"-problem.\nBut also it's pretty stupid to be required to call setContent(\"application/json\")\nwhen passing a JSONObject as parameters.\n\n\nResponse Handling (a.k.a the Transport class)\n=============================================\n\n\n### Callbacks\n\nThis is pretty straightforward if you've used prototypejs' Ajax.Request before.\n\nThere are 4 overrideable callbacks.\n\n\t// Called when a request completes without any IOExceptions thrown.\n\tonComplete(Transport)\n\t\n\t// Called when an IOException occurs and our request gets borked.\n\tonError(IOException)\n\t\n\t// Called when a request completes with an http status code less than 400\n\tonSuccess(Transport)\n\t\n\t// Called when a request completes with an http status code greater than or equal to 400\n\tonFailure(Transport)\n\nby default these callbacks do nothing so they're completely optional in case\nyou simply want to touch an url and don't care about the response. \n\n### Transport\n\nIf a request completes without any IOExceptions you will be provided with a Transport\nobject in your callback.\n\nThis object holds the Response data and all the information you normally receive from\na webserver.\n\nDepending on the response Content-Type header the Transport object will automatically try to\ndeserialize the response data into JSON or XML if possible.\nAs of now it is not recommended to use this Library to fetch binary data as it expects\ndata in string format.\n\n#### Plaintext\nTo get the raw text response content  use\n\t\n\tString data = transport.getResponseText();\n\nThis method is always available disregarding Content-Type.\n\n#### JSON\nTo get the response as an JSONObject you can use\n\t\n\tJSONObject data = transport.getResponseJson();\n \nThis method is available when Content-Type returned by the webserver is \"application/json\"\nThis method returns __null__ when the response was in another format or the response is malformed.\n\n#### XML\nDue to the fact that XML documents can be rather memory consuming - there are\n3 ways consume XML-data.\n\t\nPush parse the XML-data by providing a content handler.\n \n\tparseResponseXml(ContentHandler);\n\n\t\nPull parse the XML-data\n\n\tXmlPullParser myPullParser = getResponseXmlPullParser();\n\t\nBuild a DOM\n\n\torg.w3c.dom.Document doc = transport.getResponseXml();\n\t// Or cut right to the cheese and grab the desired nodes with:\n\torg.w3c.dom.NodeList fantasyBooks = transport.findByXpath(\"/bookstore/books[@category='fantasy']\");\n\nThis is the most memory consuming way, getResponseXml() holds a local cache to the Document\nonce built, so the node tree is only built once.\nIf the response data is malformed or fails to be parsed this method returns __null__\n  \n\nFor more info on working with Push/Pull/DOM parsers see: \n http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html\n\n\t\n \n\n\n  \n\n\n\n\n ","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelamon%2Fandroid-ajax.request","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftelamon%2Fandroid-ajax.request","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelamon%2Fandroid-ajax.request/lists"}