android facebook page like - android

i want to like a facebook page in my android app..is there any API for this?i tried a code.it was working intially.but now it is not working
likeWebView = (WebView) findViewById( R.id.webView1 );
likeWebView.getSettings().setJavaScriptEnabled(true);
String url = "http://www.facebook.com/plugins/like.php?" +
"href=" + URLEncoder.encode("likeurl" ) + "&" +
"layout=standard&" +
"show_faces=false&" +
"width=500&" +
"action=like&" +
"colorscheme=light&" +
"access_token=" + URLEncoder.encode( "read_stream" );
likeWebView.loadUrl( url );
please help me.I have been working on this for a long time.

First of all u have to create facebook developer forum account. then download facebook API which ill include in your project.
You can customized the facebook API according your requirement.
This url can help you http://forum.developers.facebook.net
:)

Related

How to post data for WebView android

I need to post data to Webview.
I found from some of the links the below code:
WebView webview = new WebView(this);
setContentView(webview);
String url = "http://www.example.com";
String postData = username=my_username&password=my_password";
webview.postUrl(url",EncodingUtils.getBytes(postData, "BASE64"));
But in my android studio I see EncodingUtils as deprecated
Can anyone help me what is the alternative for EncodingUtils to post data to Android WebView?
Try like below...
Java:
WebView webview = new WebView(this);
setContentView(webview);
String url = "http://www.example.com";
String postData = "username=" + URLEncoder.encode(my_username, "UTF-8") + "&password=" + URLEncoder.encode(my_password, "UTF-8");
webview.postUrl(url,postData.getBytes());
Kotlin:
val webview = WebView(this)
setContentView(webview)
val url = "http://www.example.com"
val postData = "username=${URLEncoder.encode(my_username, "UTF-8")}" +
"&password=${URLEncoder.encode(my_password, "UTF-8")}"
webview.postUrl(url, postData.toByteArray())
This is a simple workaround.
String html = "<!DOCTYPE html>" +
"<html>" +
"<body onload='document.frm1.submit()'>" +
"<form action='http://www.yoursite.com/postreceiver' method='post' name='frm1'>" +
" <input type='hidden' name='foo' value='12345'><br>" +
" <input type='hidden' name='bar' value='23456'><br>" +
"</form>" +
"</body>" +
"</html>";
webview.loadData(html, "text/html", "UTF-8");
I know this is not the best method but this works.
I would like to add a few things to the answer as I had to work on same and found some info could help complete the answer to this question.
First thing is the need for such a scenario. My need was that I am
creating a payment gateway client for native applications in android.
Second thing is that the URL you are opening needs to perform some
operations right. Hence you must enable your webView to enable
such operations or else things might not work. For example if your
URL is executing some java script, than you must enable java script
for your webview. This can be done as shown below :
val set = webview.settings
set.javaScriptEnabled = true
Normally this will enable trivial things such as timers, returning results etc on your webview.
Third thing is a case when your webView needs to call methods of your android app. This can be done by adding some JavaScript Interface as shown below :
webview.addJavascriptInterface(WebAppInterface(), "Android")
Where WebAppInterface() is a simple class which atleast one method annotated with #JavascriptInterface as shown below :
class WebAppInterface() {
#JavascriptInterface
fun showToast(status: String) {
//show toast here or handle status
}
}
The name Android will be the one which will be injected into your URL as a variable and you can call the methods of your android WebAppInterace from that URL as shown below:
Android.showToast("From WebPage")
Last thing is your postURL method which is somewhat like :
webview.postUrl(actionUrl, params.toByteArray(Charsets.UTF_8))
This method has couple of things that it takes as default. First is that request type is taken as default POST as the name suggest.
Header content-type can be default taken as application/x-www-form-urlencoded and
most important params it takes as & separated key value pairs as shown :
val params = "MERCHANT_ADDR=" + addr + "&CHANNEL=android"
We must pass byteArray of this string which is shown in post URL callback.
Now after your API is hit and it in some cases loads a callback url, from that call back URL using the JavaScript Interface, you can return result to your application and close the webview.
I hope it helps people.
try this:
You need to URL-encode the parameter value before sending it.
String postData = "fileContents=" + URLEncoder.encode(fileCon, "UTF-8");
For those who came here by trying to put a html body as a postData and not working,
try to put your string body as something below:
val htmlCode = "https://ramdom.user.me"
val postData = "{\n" +
"\t\"token\": \"963966f649\"\n" + "}"
webview.postUrl(htmlCode, postData.toByteArray())
I hope to save someone`s life. :-)

Selenium File Upload iOS or Android using BrowserStack

Is there any possibility to upload a file on mobile browser (Safari on iOS, Chrome on Android)? Conventional methods doesn't seem to work. Maybe there are similar options to BrowserStack where file upload can actually work?
BrowserStack uses Appium to drive your Selenium tests on Android and iOS.
As given here, since Appium currently does not support uploads, BrowserStack too wouldn't be able to support File Upload on mobile devices.
This is a hacky file upload solution using base64 and JS, but it works, so hey, I hope you find this useful:
public CorePage UploadHack(string fileInputId, string contentType, string fileContent, string fileName, string angularScopeVar)
{
UploadFile(_filePath);
var uploadHack =
"(function(){" +
"function convert(base64){" +
"var raw = atob(base64);" +
"var arr = new Uint8Array(new ArrayBuffer(raw.length));" +
"for (var i = 0; i < raw.length; i++){" +
"arr[i] = raw.charCodeAt(i);" +
"}" +
"return arr; " +
"}" +
$"var file = new Blob([convert('{fileContent}')], {{'type':'{contentType}'}}); " +
$"file.name = '{fileName}'; " +
$"angular.element('#{fileInputId}').scope().{angularScopeVar} = file;" +
"})()";
Driver.ExecuteJavaScript(uploadHack);
return this;
}

java.io.filenotfound android exception on web service call

I am getting exception java.io.filenotfound android exception on web service call in android.But when I run the same url in my browser it works fine.I am communication with the project running on local host in visual studio. My url is like this:
http://10.0.2.2:7378/HighriseeSite/appservices.svc/savewc?WcID=3646&WODetailID=480982&PSID=2084&wcdate=07/14/2015 &wcompleted=1&remarks=&rabill=0&connectivityID=0&length=0&breadth=0&height=0&execby=0
When I use the same url in my browser by replacing 10.0.2.2 with the localhost it works fine webservice gets called.But when the same url is called from the android studio project I get the exception:
java.io.filenotfound
and in the visual studio where project is running and from where the webservice is being called I get the exception as:
Object reference not set to an instance of an object.
But the same url works fine in the browser.I am not able to detect what is wrong with this.Any kind of help will be appreciated.
My code for accessing webservice is like this:
for( WCompletionData Itm:lstWC)
{
if(Float.parseFloat (Itm.getCompQty())>0.0)
{
ur = "http://" + ServerDetails.hostServer + "/appservices.svc/savewc?WcID=" + Itm.getWO_No().trim() + "&WODetailID=" + Itm.getWODetail_Id().trim() + "&PSID=" + Itm.getPS_Id().trim() + "&wcdate=" + currentDateTimeString.trim().trim() + " &wcompleted=" + Itm.getCompQty().trim() + "&remarks=&rabill=" + Itm.getRABill_Id().trim() + "&connectivityID=0&length=0&breadth=0&height=0&execby=0";
//String ur = "http://"+ServerDetails.hostServer+"/appservices.svc/TaskQuanityList?MaterialID="+MatrID+"&ProjectNo="+ProID+"&TaskNo="+TaskID;
Log.d("URLgggrn", ur);
// Replace it with your own WCF service path
URL json = new URL(ur);
URLConnection jc = json.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
line = reader.readLine().toString();
Log.d("Line", line);
WCNo = WCNo + line + ",";
}
I am using same code on another activity and it works fine on that activity.
To get rid of File Not Found Exception while requesting to a URL
from android we should avoid white space from URL. If you need to pass
parameter data which contain space you have to use below code
String param1 = "happy coding";
String url = "http://example.com/query?q=" + URLEncoder.encode(param1 , "UTF-8");

Is there a locationChange equlivent for PhoneGap InAppBrowser like in ChildBrowser

I am trying to use Google OAuth2 for Google Drive authentication on both Android and iOS. I had a solution working on Android using ChildBrowser, but it did not work on iOS. PhoneGap Build support suggested I use InAppBrowser because ChildBrowser is depreciated.
I can get iOS and Android both to prompt my user for ID/PW then it shows "Allow Access" button all in InAppBrowser to give Googl Drive access. On Android tapping Allow Access button gives page not available error with correct code and OAuth token in the url. When using childbrowser it was this change event that allowed you to check for code 4 and get the token. InAppBrowser only has LoadStart, LoadStop and Exit events.
How can I check the Google return URL for success/failure and if success grab the token?
Thanks for any and all help!
Here's how I use InAppBrowser to do Facebook auth. Since you're just trying to grab a value out of the url, it should work the same way:
var fb = {
id: '...'
,secret: '...'
,namespace: '...'
,redirect: '...'
,display: 'touch'
,scope: ['publish_actions']
,windowRef: null
,code: null
,onLoadEvent: function(event){
var url = event.url;
console.log(url);
//once we've been redirected to this domain, fb's job is done
if (/my.domain.com/.test(url)){
if (/code=/.test(url)){
//auth done
var code = url.match(/code=([^\&]+)/)
if (code) code = code[1];
window.fb.code = code;
window.fb.windowRef.close();
}else if (/error_description=/.test(url)){
//login unsuccessful
var error = url.match(/error_description=([^\&]+)/);
if (error) error = error[1].replace(/[^A-Z0-9 ]/gi, " ");
console.error("facebook login failed: " + error);
window.fb.code = null;
window.fb.windowRef.close();
}
}
}
};
var authUrl = "https://graph.facebook.com/oauth/authorize?"
+ "client_id=" + fb.id
+ "&redirect_uri=" + encodeURI(fb.redirect)
+ "&display=" + fb.display
+ "&scope=" + fb.scope.join('%2C');
fb.windowRef = window.open(authUrl, '_blank', loginPopupOptions);
//no reason we can't use the same function twice...
fb.windowRef.addEventListener('loadstart', fb.onLoadEvent);
fb.windowRef.addEventListener('loadstop', fb.onLoadEvent);
I tried to pull only the relevant bits out of our structure so hopefully it makes sense this way...

AIR Actionscript 3 Adding facebook share button

basically, I'm currently building a mobile game using Actionscript 3.0 and AIR (Flash Pro 6). I want to add a facebook share button which the user can post their achievements to their wall.
The post will contains some words and an image.
I didn't know where to start. Can someone please help? thx b4...
This should help:
http://code.google.com/p/facebook-actionscript-api/
There is also documentation here:
http://code.google.com/p/facebook-actionscript-api/downloads/detail?name=GraphAPI_Documentation_1_8_1.zip&can=2&q=
You can use this AS3 code to use FB share button.
fbShareBtn.addEventListener(MouseEvent. CLICK, shareapp)
function shareapp(evt:Event):void
{
var req:URLRequest = new URLRequest();
req.url = "http://www.facebook.com/dialog/feed";
var vars:URLVariables = new URLVariables();
vars.app_id = "xxxxxxxxxxxx"; // your application's id
vars.link = "http://youromainhere";
vars.picture = "http://yourimage";
vars.name = "My Game";
vars.caption = "My Score ";
//vars.description = "My score is " + String(score) + " Try and you!";
vars.redirect_uri = "http://yourdomainhere";
req.data = vars;
req.method = URLRequestMethod.GET;
navigateToURL(req, "_blank");
}

Categories

Resources