First of all. Sorry for my bad enghlish.
I want to write a program what can communicate with my website with post/get parameters but when i run the progress httppost with http://xyz.xyz/?server&user=this&pass=that
it's not work... how can i fix this problem if i can do that.
the sintax
GET
username
password
POST
message
captcha
I know exists two method for this but i don't know one progress for these two method with together.
(httppost / httpget methods; but together... i need it)
Thank you for helping
I did not fully understand your question, but if you want to login to a website (HTTP POST and all), the below link might help.
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
Also, note, the "stringdata" and "id" given in the example is the id of the input parameter in the html code.
<input id="___THIS HERE___" value="..etc" >
According to your question, I'd say you do it like this:
...
nameValuePairs.add(new BasicNameValuePair("user", "this"));
nameValuePairs.add(new BasicNameValuePair("pass", "that"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
...
Related
Goal
I am writing a program where a user's input is taken as a parameter and queried against an online API.
Problem
Oddly, I cannot get my parameter into my API successfully. The error I get is
"Could not look up user information; You have an error in your SQL syntax;" Which as it says plainly , is an SQL error. Therefore I was thinking there was a problem in passing my parameter since the application works when I hard code parameter and say "select name from table where id=1".
This is the parameter code and despite many edits and changes I got the same issue which caused me to look to my php even if everything works right in the browser.
HttpParams param = new BasicHttpParams();
ArrayList<NameValuePair> inputArguments = new ArrayList<NameValuePair>();
inputArguments.add(new BasicNameValuePair("id", idnum));
HttpClient client = new DefaultHttpClient(param);
HttpPost request = new HttpPost("http://myurl.com/DAIIS/getName.php");
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setEntity(new UrlEncodedFormEntity(inputArguments, "UTF-8"));
HttpResponse httpResponse = (HttpResponse) client.execute(request);
Where I think the problem lie
I belives the problem lies in my select statement
<?php
header("Content-Type:application/json");
//Connect to DB
include ("dbcon.php");
//Run query
$para=$_GET['id'];
$sql=("SELECT name FROM class where stu_id=$para");
I say this because after stripping my API to the bare minimum the program's error was Could not look up user information; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
but if i hard code the parameter (it works) or put something random like stu_id=$_GET['id']; it returns blank.
So is the way that I used this parameter incorrect for android? even if it works in the browser?
Thank you
As you asked for :
Just change '$_GET' to '$_POST',
As a side note
You can also check 'POST' request in browser, in order to do that add 'Rest client plugin' to your browser and you are done and have fun with api calls :)
I need to post data to webservice where I need to send a parameter along with JSON object in android . Is it possible to send both parameter and JSON object while posting data ?
My code is as folows:
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("token", "abc.org"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpPost.setEntity(JSON.toString);
Thanks in Advance
You can't call setEntity() twice like this. If your server is expecting POST content that is form-encoded, then you'll need to add your JSON object as one of the POST parameters. If your server is expecting the JSON object as the POST body, then it can't also be expecting any other parameters (this wouldn't be possible). It may be possible that it expects additional parameters in the query string part of the URL though. Check the API documentation of your server.
I send some data (utf8) in my dataBase and I'm seeing them from phpMyAdmin. The data contains greek and english characters. The problem is that greek characters are showing up as question marks(????). The english are ok. No matter what I have tried:
For dataBase and my column collation: utf8_unicode_ci, utf8_general_ci
After my connection with dataBase:
mysqli_query ("SET NAMES 'UTF8'", $dbc);
mysqli_query ("SET CHARACTER SET 'UTF8'", $dbc);
the problem still remains and I can't figure out how to solve it. Does somebody knows how to fix that?
Update:
The data are sent from an Android application:
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("a", "hello, ΓΕΙΑ ΣΟΥ" ));
nameValuePairs.add(new BasicNameValuePair("b", "Good Morning, ΚΑΛΗΜΕΡΑ"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try {
httpclient.execute(httppost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} catch (IOException e) {
Log.i("HTTP Failed", e.toString());
}
And the command to insert values into database:
$q= 'INSERT INTO `table`(`x`, `y`) VALUES ("'.$_POST["a"].'","'.$_POST["b"].'")';
Check that your db has UTF8 encoding enabled for the db/tables
Check on browser that UTF8 is enabled (e.g. for Firefox: View/Character Encoding/Unicode (UTF-8))
Check reponse Content-type http header:
Content-Type: text/html; charset=utf-8
This is a FAQ so the phpMyAdmin team has written this page to help:
http://wiki.phpmyadmin.net/pma/Garbled_data
I had the same problem, I searched and read this post.
After I have found a solution to my problem I want to share it.
change this line of your code
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
to
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
and now you are ok.
George, at first I thought that your code was in PHP, taking into consideration that you mentioned phpMyAdmin. Although, I do have a little experience in Android, and I would avoid writing Greek characters (or any non-standard characters) in a .java file.
Instead, you should use the strings.xml file that Android supports, and access your strings in the code like this:
R.string.kalimera
where kalimera is the name of the variable you use for your "Hello" text.
That would perfectly explain why the database is filled with question-marks (????) instead of the actual characters (or at least so I think).
I have connected my database from sql server to my android app and get an output and everything runs fine.I just need help with a register page where I want to save data that has been entered into edittext boxes tothe database but cant seen to get the correct method for this. Any help or guide lines would be very much appreciated thanx :-D
Have a look at DBHelper class, Hope it solves your problem
Oh, forgot I had some files on my mail :).
Here is a little example:
Java (android):
nameValuePairs.add(new BasicNameValuePair("Username","%"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("location php file");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
Php:
mysql_query("INSERT INTO Persons (UserName) VALUES '".$_REQUEST['Username']."'");
I thought it was something like this. You may need to change it a little, but you have a start here.
Seemingly simple question, but no obvious answer found online.
At this link, there is a tutorial on posting simple name value pairs to a file from within an android app. http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
What I want to do is post 'something' which on the receiving end (a php script) can be accessed as an array.
In PHP I want to receive:
$_POST['array']=array("key"=>"value","key2"=>"value2");
Being relatively new to android development, perhaps someone could elaborate on creating a similar thing in Java, and then how one cant send it - setEntity seems to only take namevaluepairs...
Many Thanks
You should use a JSON Wrapper both in Android App and your PHP server.
In PHP you should use json_decode(), like: $thingFromPost = json_decode($data).
In Java, there are many ways to create a JSONArray. A basic example would be:
List<String> list = new ArrayList<String>();
list.add("foo");
list.add("bar");
JSONArray jsonArray = new JSONArray(list);
And after that, you just send your array with a HttpPost to your server.
StringEntity stringEntity = new StringEntity(jsonArray.toString());
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING, "UTF-8"));
stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
HttpPost post = new HttpPost(url);
post.setEntity(stringEntity);
post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
If you need a detailed tutorial how to make requests using JSON in Android, follow this link.
Hope it helps!
If you want the entire, raw body of your POST to be the stringified array (and nothing else), I believe you should use a StringEntity instead of a UrlEncodedFormEntity.
So this:
String s = "asdf"; // replace this with your JSON string
StringEntity stringEntity = new StringEntity(s);
httpPost.setEntity(stringEntity);
I am not familiar with PHP, but conceptually on the receiving end you'll then do something like json.parse(request.full_body). Note that this (request.full_body or the equivalent) is very different from the common pattern of fetching a single value of the POST form like request['input_field1'].
However, reading your question I'm not entirely sure that this full_body approach is what you want. It looks to me like you want to access the data via the form variable 'array', as you indicate here:
$_POST['array']=array("key"=>"value","key2"=>"value2");
Note that you are not working with the entire POST body here, rather instead you are fetching the value of a single form variable called 'array' (I think, I don't really know PHP). If this is the case, then you should use NameValuePairs like something below:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("array", yourJSONArrayAsString));
This will post the array as a value associated with the form variable 'array' I believe.