Http Requests In Glass Gdk
I am implementing a GDK application and need to do in my application some HTTP Post requests. Do I send the HTTP requests the same way as on android phone or there is some other wa
Solution 1:
You can make any post request like in smartphones, but ensure you make the requests using an AsyncTask.
For example:
privateclassSendPostTaskextendsAsyncTask<Void, Void, Void> {
@OverrideprotectedVoiddoInBackground(Void... params) {
// Make your request POST here. Example:myRequestPost();
returnnull;
}
protectedvoidonPostExecute(Void result) {
// Do something when finished.
}
}
And you can call that asynctask anywhere with:
new SendPostTask().execute();
And example of myRequestPost() may be:
privateintmyRequestPost() {
intresultCode=0;
Stringurl="http://your-url-here";
HttpClientclient=newDefaultHttpClient();
HttpPostpost=newHttpPost(url);
// add headers you want, example:// post.setHeader("Authorization", "YOUR-TOKEN");
List<NameValuePair> urlParameters = newArrayList<NameValuePair>();
nameValuePairs.add(newBasicNameValuePair("id", "111111"));
nameValuePairs.add(newBasicNameValuePair("otherField", "your-other-data"));
try {
post.setEntity(newUrlEncodedFormEntity(urlParameters));
HttpResponseresponse= client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
resultCode = response.getStatusLine().getStatusCode();
BufferedReaderrd=newBufferedReader(
newInputStreamReader(response.getEntity().getContent()));
StringBufferresult=newStringBuffer();
Stringline="";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
} catch (Exception e) {
Log.e("POST", e.getMessage());
}
return resultCode;
}
Post a Comment for "Http Requests In Glass Gdk"