I have an android app that calls a restful API in PHP that saves information in mysql. The URL contains Arabic characters in one of its parameters.
When I send this URL from the browser itself, I eventually see the Arabic characters in the DB (and when echoing) but when I call this URL from the app, it echos back (I added echo command on the php side) and store in the DB "?????".
When calling the API, I do the following:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");
urlConnection.setRequestProperty("accept-charset", "UTF-8");
Am I missing something?
Related
I have a problem with php file when it should receive some data form my android app. I'm using POST method and I can't figure it out why it doesn't work. I've tried searching for answers but everything I tried, it doesn' work. First, I used URLConnection class for connection with server, and after that, I tried with HttpURLConnection, but still doesn't work. I want to transfer data to server exclusively with the POST method, not GET. Could anyone explane me what I'm doing wrong, or did I forget something to add or configure?
Did you try something like this :
URL url = new URL("your_url_here");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.connect();
byte[] output = "your data to send here".getBytes("UTF-8");
OutputStream os = connection.getOutputStream();
os.write(output);
os.flush();
os.close();
I have an android app and am trying to send data to the PHP on the server. The server gets the php data with
$this->get('uname');
$this->get('pass');
We do use codeigniter if that matters
The Java code, inside of an Async method, I currently have is
InputStream response = null;
URLConnection connection = new URL(urls[0]).openConnection();
connection.setRequestProperty("uname" , Username);
connection.setRequestProperty("pass", Password);
response = connection.getInputStream();
When I run this, the code always returns null but it is supposed to return a JSON array either way. I have checked the URL and it is correct. What am I doing wrong? Thanks!
You code should be like this
For your android side ,as mentioned by #Ichigo Kurosaki at Sending POST data in Android
For Codeigniter side , cosider you function name is user_login
function user_login(){
$uname = $this->input->get_post('uname'); //get_post will work for both type of GET/POST request
$pass = $this->input->get_post('pass');
$result=$this->authenticate->actLogin($uname,$pass ); //considering your authenticating user and returning 1,0 array as success/failure
$this->output
->set_content_type('application/json')
->set_output(json_encode($result));
exit;
}
I am attempting to get my Access database written in VBA to communicate with a MySQL database on my clients website through a set of PHP web services I wrote. I have managed to get the Access db to retrieve data from the MySQL db but can't get it to post anything. I have narrowed the problem down to the fact that my HTTP request isn't sending the arguments I assign it.
Here are some questions and sites I have looked at already. These were not helpful, because the majority weren't dealing with PHP and were instead looking directly at websites, or were dealing with GET rather than POST.
Http Post not posting data
excel vba http request download data from yahoo finance
VBA HTTP GET request - cookies with colons
Pass Parameters in VBA HTTP Post Request
Perform hidden http-request from vba
How can I send an HTTP POST request to a server from Excel using VBA?
http://www.tushar-mehta.com/publish_train/xl_vba_cases/vba_web_pages_services/
https://stackoverflow.com/questions/29190759/vba-oauth2-authentication-http-get-request
VBA ServerXMLHTTP https request with self signed certificate
How can I send an HTTP POST request to a server from Excel using VBA?
Sending http requests with VBA from Word Sending http requests with VBA from Word
My code for VBA is:
Dim strJSONEncodedJob As String
strJSONEncodedJob = "[{""ExpenseID"":""" & astrExpenseIDs(intI) & "}]"
URL = "I removed the URL when posting"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "Content-Type", "application/json"
objHTTP.send (strJSONEncodedJob)
strResponse = objHTTP.responseText
MsgBox strResponse
And my PHP code is:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$json = file_get_contents('php://input');
$data = json_decode($json, true);
$stmt = $conn->prepare("DELETE FROM tblExpenses WHERE ExpenseID=?");
$txtExpenseID = $data['ExpenseID'];
$stmt->bind_param("i", $txtExpenseID);
$stmt->execute();
echo '{"result" : "success"}';
$stmt->close();
$conn->close();
I get the success statement as a msgbox in VBA, as expected, however the record is not deleted from the MySQL db.
Does anyone have a solution?
Thanks in advance.
Update
When I echo $json I get the JSON encoded string, which means the arguments ARE being passed. However, when I echo $data['ExpenseID'] I get a blank msgbox.
Your json-encoded object is an array containing a single object, so if you want to get at that object's properties you'd first have to index it from the array. Something like
$txtExpenseID = $data[0]['ExpenseID'];
(again, I'm not a PHP person so just guessing the syntax)
I am developing an android application where the user data should be pushed onto the server .How can i do that ? I was able to get data from the server but unable to insert it. Code snippet would be good.I have tried this but it doesn't work.
Java Code :
String data="xyz.org/json.php"+"?"+"&"+URLEncoder.encode("data", "UTF-8") + "="+"order";
URL url;
url = new URL(data);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
In json.php :
$data = urldecode($_GET['data']);
if($data=="order")
{
$query="insert into xyz values('$x','$y','$z')";
$query_run=mysql_query($query);
}
you need a httppost or httpget dependance on the your url method for that you can use Httppost and get follow this url you get some idea and also check this url for asynctask beacuse you have to put all the network related you have to put in the asynctask.
I've written the code to post the information to a page, however I don't know how to make the post target the iframe I've placed on the page. (I haven't tested this code so I'm not sure if the post even works)
Dim param1, param2, result, url As String
Dim request As HttpWebRequest
Dim paramStream() As Byte
Dim requestResponse As WebResponse
param1= "name=" + Server.UrlEncode("My Name")
param2= "email=" + Server.UrlEncode("email#mail.com")
paramStream = Encoding.ASCII.GetBytes(param1+ "&" + param2)
url = "https://www.mysite.com/dosomething.php"
request = WebRequest.Create(url)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; sv-SE; rv:1.9.1b2) Gecko/20081201 Firefox/3.1b2"
request.ContentLength = paramStream.Length
Using stream = request.GetRequestStream()
stream.Write(paramStream, 0, paramStream.Length)
End Using
requestResponse = request.GetResponse()
Using sr = New StreamReader(requestResponse.GetResponseStream())
result = sr.ReadToEnd()
End Using
That is what I have written to post to the website, however I would like the post to affect an iframe which I have placed on the page. I know when you're writing html you can have a target specified in the form tag
<form target="my_iframe" method="post" action="dosomething.php" />
But I wasn't sure if there is one similar to target that I can specify from VB code.
Just a note: I am trying to do a post from a ASP.NET page to a PHP page that is contained in an iframe.
I have not tested that this even works to post yet, if you see anything in my code that needs fixing up or have any suggestions for how to do this a different way please let me know.
When you're screen-scraping using HttpWebRequest, you're working at the HTTP layer, underneath the HTML layer that browsers are concerned about. HTTP does not know or care about frames or any other HTML elements layered on top. HTTP simply sends requests to URLs and gets responses back. So you simply need to send the POST request to the URL of the IFRAME. The fact that normally that URL lives in an IFrame shouldn't matter to your VB code.