Reputation: 4659
So I have this Android Application that's in WebView that should pass the data to a server(hosting site), unfortunately I can't do it so I need your help to solve this problem.
I got most of the code from this site so, it might looked familiar to you.
So I have this HTML file that initiate the function to send the data to a server.
watch_video.html
var v_id = getUrlVars()["v_id"];
$.ajax({
url: "http://mysite.com/fbtest.php?json_callback=?",
dataType: "jsonp text",
//GET method is used
type: "POST",
//pass the data
data: 'v_id=' + v_id,
//Do not cache the page
cache: false,
//Cross Domain
crossDomain: true,
//success
success: function (html) {
alert(html);
}
So in PHP file which in a server(hosting site) that uses cross domain here's the code:
<?php
include("includes/db.php");
header('content-type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
$v_id = ($_POST['v_id']) ?$_POST['v_id'] : $_GET['v_id'];
$dwatch=date("Y-m-d");
$twatch=date("H:i:s");
$query="insert into watched_videos (fb_id,vid,date_watched,time_watched) values('12345','".$v_id."','".$dwatch."','".$twatch."')";
$row = mysql_query($query);
?>
I tested this locally using XAMPP, it works it inserted the data in a database.
When I installed it in Android, and did the same function it didn't worked.
Here's my MainActivity.java code:
package namename.name;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
public class MainActivity extends Activity {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webtest);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/www/indexbackup.html");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webtest);
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
So going back to what I've said before I initiated the function in WebView using JSONP it did not work.
I was wondering if I have something to put in my android file or so.
I would be glad if someone helps me.
Thanks in advance.
Upvotes: 1
Views: 1886
Reputation: 30488
the problem is with this line
mWebView.loadUrl("file:///android_asset/www/indexbackup.html");
you have to give proper file path like a browser
mWebView.loadUrl("http://yoursite/android_asset/www/indexbackup.html");
Upvotes: 1