I have existing PHP that accepts arrays as parameters. This is currently being invoked from javascript and the javascript array is being set directly as part of the data object sent through AJAX call to the php.
Now, I want to send an array from an Android app and I don't want to change the existing php and javascript. I have looked for an answer, but don't see anything, other than the suggestion to encode the array as JSON and decode it in the php, but that will require lots of changes everywhere, including the javascript. It appears the javascript and the php are made to just work. Maybe something is done automatically to the array?
The PHP looks like this:
foreach($_POST as $key => $value) {
if (is_array($_POST[$key])){
for ($index = 0; $index < count($_POST[$key]); $index++){
if(ini_get('magic_quotes_gpc'))
$_POST[$key][$index] = stripslashes($_POST[$key][$index]);
$_POST[$key][$index] = htmlspecialchars(strip_tags($_POST[$key][$index]));
}
}
else {
if(ini_get('magic_quotes_gpc'))
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
}
So, it is definitely expecting some parameters to be arrays.
The javascript currently does this:
$.ajax({
type: "POST",
url: 'php/getLibraryPatterns.php',
dataType: 'json',
data: data,
and data is a javascript object with arrays contained in it. For example:
var data = {};
if (sortBy.length > 0){
data.sortBy = sortBy;
}
and sortBy is an array in the javascript.
How do I pass an array from the Android app to this php? Currently, all of the parameters for my other calls to php have just been simple strings, so I use the following function to put all the parameters together in a string and then write that to the OutputStream of my HttpURLConnection. I have that all working, just not sure what to do with the arrays?
private String buildParameterString() throws UnsupportedEncodingException
{
if (m_parameterMap == null) return "";
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<Map.Entry<String, String>> itr = m_parameterMap.entrySet().iterator();
while(itr.hasNext())
{
Map.Entry<String, String> param = itr.next();
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(param.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(param.getValue(), "UTF-8"));
}
return result.toString();
}
I tried creating a JSONArray with the data, then convert to string and set it just like I would set the other strings I am sending. But, this does not appear to work, at least not automagically.
So, I am sending something like "[29]" or "['sortAsc']" as strings in the parameters.
I got back an empty list. I will continue to debug in the php to see what those look like there, but I expect they will be just strings and the php won't know what to do with that.
So, how can I send an array to the php? I don't want to have to change the php, as that means that the javascript then also has to change. Is there any way to do this from a java Android app?
Solution Found!
I found an answer in the JQuery documentation for ajax method. It says there that arrays are encoded like this:
%5B is '['
and %5D is ']'
For example, { a: [1,2] } becomes the string "a%5B%5D=1&a%5B%5D=2" with the default traditional: false setting.
I have encoded my arrays like that in the string that I write to the output stream. From https://api.jquery.com/jQuery.ajax/
Here is the code I am using to create my output stream:
private String buildParameterString() throws UnsupportedEncodingException
{
if (m_parameterMap == null || m_parameterMap.size() == 0) return "";
StringBuilder result = new StringBuilder();
for (int iPair = 0; iPair < m_parameterMap.size(); iPair++)
{
Pair<String, Object> param = m_parameterMap.get(iPair);
if (iPair > 0) result.append("&");
Object value = param.second;
if (value instanceof ArrayList){
for (int i = 0; i < ((ArrayList)value).size(); i++){
Object nextValue = ((ArrayList)value).get(i);
String nextValueStr = "";
if (nextValue instanceof String){
nextValueStr = (String)nextValue;
}
else {
nextValueStr = Integer.toString((Integer)nextValue);
}
if (i > 0) result.append("&");
result.append(URLEncoder.encode(param.first, "UTF-8"));
result.append("%5B%5D=");
result.append(URLEncoder.encode(nextValueStr, "UTF-8"));
}
}
else {
result.append(URLEncoder.encode(param.first, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode((String)value, "UTF-8"));
}
}
return result.toString();
}
m_parameters is an ArrayList of Pair's where Pair is in android.util.
It works, this is the answer. You don't have to encode and decode in JSON string to do this. All the othere answers I have seen for this are just that. There is a way to encode the array into the string output.
Try this:
Outside of while loop:
JSONArray data = new JSONArray();
And put this in while loop:
JSONObject jsonObjSend = new JSONObject();
jsonObjSend.put(param.getKey(), param.getValue());
data.put(jsonObjSend);
From Android, send it as json data
public String convertMapToJson() {
Map<String, String> elements = new HashMap<>();
elements.put("Key1", "Value1");
elements.put("Key2", "Value2");
elements.put("Key3", "Value3");
JSONObject json = new JSONObject(elements);
return json.toString();
}
In server side PHP,
$json = file_get_contents('php://input');
// Converts it into a PHP object
$params = json_decode($json, true);
echo $params["Key1"];
I have got a HTTP request in my Swift code that sends a POST request to my PHP code.Here is my php code
class Comment
{
public $CommenterName;
public $CommentDate;
public $CommentLikes;
function __construct($CommenterName,$CommentDate,$CommentLikes)
{
$this->CommenterName = $CommenterName;
$this->CommentDate = $CommentDate;
$this->CommentLikes = $CommentLikes;
}
}
And my php codes returns an array full of Comment objects.Now when i receive this in swift i do something like this
let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as! [Any]
print(jsonData[0])
Printing this returns me
{
CommentDate = "2017-06-29 01:21:57";
CommentLikes = 2;
CommenterName = muradsh;
}
And when i want to access one of this objects like this
print(jsonData[0][2])
or this
print(jsonData[0]["CommenterName"])
it returns me this following error Type 'Any' has no subscript members.
How can i get access to CommenterName in jsonData?
Since the data you get is really an array of objects try casting it like this:
let jsonData = try JSONSerialization.jsonObject(with: data, options: []) as! [[String: Any]]
PHP file stored in www directory of WAMP
<?php
//Response array JSON
$response = array();
//Connexion to the db local in WAMP
include("connexion_db.php");
$idcom = connexobject("database_app_android","myparam");
$sql = "SELECT * FROM products";
$result = $idcom->query($sql);
if(!$result)
{
//Cannot read
$response["success"] = 0;
$response["mesage"] = "SQL query has timed out";
}
else
{
if($result->num_rows > 0)
{
//Products founds
$response["products"] = array();
while($row = $result->fecth_object())
{
$product = array();
foreach ($row as $key => $value)
{
$product[$key] = $value;
}
//Add product to the response JSON array
array_push($response["products"], $product);
}
$response["success"] = 1;
}
else
{
//0 product found
$response["success"] = 0;
$response["mesage"] = "No products found";
}
}
//Encode and send response in JSON format
echo(json_encode($response));
$response->free();
$result->free();
$idcom->close();
?>
Android API
public interface CatalogAPI {
#GET("/products")
Call<List<Product>> getProductList();
#GET("/products/{product}")
Call<Product> getProduct(#Path("product") int productId);
}
Android part using retrofit
//Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:3000/MyAndroidApp/get_all_product.php")
.addConverterFactory(GsonConverterFactory.create())
.build();
CatalogAPI service = retrofit.create(CatalogAPI.class);
service.getProductList().enqueue(new Callback<List<Product>>() {
#Override
public void onResponse(Call<List<Product>> call, Response<List<Product>> response) {
Log.i("retrofit","DOWNLOAD OK");
for (Product p : response.body())
{
Log.i("retrofit","Product : " + p.getName());
catalog.add(p);
}
}
#Override
public void onFailure(Call<List<Product>> call, Throwable t) {
}
});
Please, why this doesn't work ? I use the Android studio emulator. The php script encode to json all products founds in the mysql database. The MySQL database is on a local server wamp
Please add logging interceptor to your retrofit client, this would help to find where is the problem .
Gradle Change
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
Code Change
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// add your other interceptors …
// add logging as last interceptor
httpClient.addInterceptor(logging); // <-- this is the important line!
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
In php file write
while($row = $result->**fetch**_object())
instead of
while($row = $result->**fecth**_object())
Good day, i know this is coming late though, but the problem seems to come from the way PHP encodes into json array.
PHP adds extra features like "\n\r" and more to encode into json array, which is totally different from what Retrofit 2 is expecting.
Retrofit 2 expects something like:
[
{ "name": "John Doe", "address": "60, station road", "mobile": "1" },
{ "name": "Doe Chujwu", "address": "6, Morgan Estate", "mobile": "2" }
].
Hence change your output to echo the above give format and then would your code work.
I hope it helps.
NB: also test that you can access your server homepage on the emulators browser. (This is to ensure that your are using the right ipaddress to connect).
I would like to parse a string such as p1=6&p2=7&p3=8 into a NameValueCollection.
What is the most elegant way of doing this when you don't have access to the Page.Request object?
There's a built-in .NET utility for this: HttpUtility.ParseQueryString
// C#
NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);
' VB.NET
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(querystring)
You may need to replace querystring with new Uri(fullUrl).Query.
HttpUtility.ParseQueryString will work as long as you are in a web app or don't mind including a dependency on System.Web. Another way to do this is:
NameValueCollection queryParameters = new NameValueCollection();
string[] querySegments = queryString.Split('&');
foreach(string segment in querySegments)
{
string[] parts = segment.Split('=');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { '?', ' ' });
string val = parts[1].Trim();
queryParameters.Add(key, val);
}
}
A lot of the answers are providing custom examples because of the accepted answer's dependency on System.Web. From the Microsoft.AspNet.WebApi.Client NuGet package there is a UriExtensions.ParseQueryString, method that can also be used:
var uri = new Uri("https://stackoverflow.com/a/22167748?p1=6&p2=7&p3=8");
NameValueCollection query = uri.ParseQueryString();
So if you want to avoid the System.Web dependency and don't want to roll your own, this is a good option.
I wanted to remove the dependency on System.Web so that I could parse the query string of a ClickOnce deployment, while having the prerequisites limited to the "Client-only Framework Subset".
I liked rp's answer. I added some additional logic.
public static NameValueCollection ParseQueryString(string s)
{
NameValueCollection nvc = new NameValueCollection();
// remove anything other than query string from url
if(s.Contains("?"))
{
s = s.Substring(s.IndexOf('?') + 1);
}
foreach (string vp in Regex.Split(s, "&"))
{
string[] singlePair = Regex.Split(vp, "=");
if (singlePair.Length == 2)
{
nvc.Add(singlePair[0], singlePair[1]);
}
else
{
// only one key with no value specified in query string
nvc.Add(singlePair[0], string.Empty);
}
}
return nvc;
}
To do this without System.Web, without writing it yourself, and without additional NuGet packages:
Add a reference to System.Net.Http.Formatting
Add using System.Net.Http;
Use this code:
new Uri(uri).ParseQueryString()
https://msdn.microsoft.com/en-us/library/system.net.http.uriextensions(v=vs.118).aspx
I needed a function that is a little more versatile than what was provided already when working with OLSC queries.
Values may contain multiple equal signs
Decode encoded characters in both name and value
Capable of running on Client Framework
Capable of running on Mobile Framework.
Here is my solution:
Public Shared Function ParseQueryString(ByVal uri As Uri) As System.Collections.Specialized.NameValueCollection
Dim result = New System.Collections.Specialized.NameValueCollection(4)
Dim query = uri.Query
If Not String.IsNullOrEmpty(query) Then
Dim pairs = query.Substring(1).Split("&"c)
For Each pair In pairs
Dim parts = pair.Split({"="c}, 2)
Dim name = System.Uri.UnescapeDataString(parts(0))
Dim value = If(parts.Length = 1, String.Empty,
System.Uri.UnescapeDataString(parts(1)))
result.Add(name, value)
Next
End If
Return result
End Function
It may not be a bad idea to tack <Extension()> on that too to add the capability to Uri itself.
If you don't want the System.Web dependency, just paste this source code from HttpUtility class.
I just whipped this together from the source code of Mono. It contains the HttpUtility and all it's dependencies (like IHtmlString, Helpers, HttpEncoder, HttpQSCollection).
Then use HttpUtility.ParseQueryString.
https://gist.github.com/bjorn-ali-goransson/b04a7c44808bb2de8cca3fc9a3762f9c
If you want to avoid the dependency on System.Web that is required to use HttpUtility.ParseQueryString, you could use the Uri extension method ParseQueryString found in System.Net.Http.
Make sure to add a reference (if you haven't already) to System.Net.Http in your project.
Note that you have to convert the response body to a valid Uri so that ParseQueryString (in System.Net.Http)works.
string body = "value1=randomvalue1&value2=randomValue2";
// "http://localhost/query?" is added to the string "body" in order to create a valid Uri.
string urlBody = "http://localhost/query?" + body;
NameValueCollection coll = new Uri(urlBody).ParseQueryString();
I just realized that Web API Client has a ParseQueryString extension method that works on a Uri and returns a HttpValueCollection:
var parameters = uri.ParseQueryString();
string foo = parameters["foo"];
private void button1_Click( object sender, EventArgs e )
{
string s = #"p1=6&p2=7&p3=8";
NameValueCollection nvc = new NameValueCollection();
foreach ( string vp in Regex.Split( s, "&" ) )
{
string[] singlePair = Regex.Split( vp, "=" );
if ( singlePair.Length == 2 )
{
nvc.Add( singlePair[ 0 ], singlePair[ 1 ] );
}
}
}
Just access Request.QueryString. AllKeys mentioned as another answer just gets you an array of keys.
HttpUtility.ParseQueryString(Request.Url.Query) return is HttpValueCollection (internal class). It inherits from NameValueCollection.
var qs = HttpUtility.ParseQueryString(Request.Url.Query);
qs.Remove("foo");
string url = "~/Default.aspx";
if (qs.Count > 0)
url = url + "?" + qs.ToString();
Response.Redirect(url);
Since everyone seems to be pasting his solution.. here's mine :-)
I needed this from within a class library without System.Web to fetch id parameters from stored hyperlinks.
Thought I'd share because I find this solution faster and better looking.
public static class Statics
public static Dictionary<string, string> QueryParse(string url)
{
Dictionary<string, string> qDict = new Dictionary<string, string>();
foreach (string qPair in url.Substring(url.IndexOf('?') + 1).Split('&'))
{
string[] qVal = qPair.Split('=');
qDict.Add(qVal[0], Uri.UnescapeDataString(qVal[1]));
}
return qDict;
}
public static string QueryGet(string url, string param)
{
var qDict = QueryParse(url);
return qDict[param];
}
}
Usage:
Statics.QueryGet(url, "id")
Hit up Request.QueryString.Keys for a NameValueCollection of all query string parameters.
To get all Querystring values try this:
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(querystring)
Dim sb As New StringBuilder("<br />")
For Each s As String In qscoll.AllKeys
Response.Write(s & " - " & qscoll(s) & "<br />")
Next s
var q = Request.QueryString;
NameValueCollection qscoll = HttpUtility.ParseQueryString(q.ToString());
I translate to C# version of josh-brown in VB
private System.Collections.Specialized.NameValueCollection ParseQueryString(Uri uri)
{
var result = new System.Collections.Specialized.NameValueCollection(4);
var query = uri.Query;
if (!String.IsNullOrEmpty(query))
{
var pairs = query.Substring(1).Split("&".ToCharArray());
foreach (var pair in pairs)
{
var parts = pair.Split("=".ToCharArray(), 2);
var name = System.Uri.UnescapeDataString(parts[0]);
var value = (parts.Length == 1) ? String.Empty : System.Uri.UnescapeDataString(parts[1]);
result.Add(name, value);
}
}
return result;
}
let search = window.location.search;
console.log(search);
let qString = search.substring(1);
while(qString.indexOf("+") !== -1)
qString = qString.replace("+", "");
let qArray = qString.split("&");
let values = [];
for(let i = 0; i < qArray.length; i++){
let pos = qArray[i].search("=");
let keyVal = qArray[i].substring(0, pos);
let dataVal = qArray[i].substring(pos + 1);
dataVal = decodeURIComponent(dataVal);
values[keyVal] = dataVal;
}
This is my code, I think it's very useful:
public String GetQueryString(string ItemToRemoveOrInsert = null, string InsertValue = null )
{
System.Collections.Specialized.NameValueCollection filtered = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (ItemToRemoveOrInsert != null)
{
filtered.Remove(ItemToRemoveOrInsert);
if (!string.IsNullOrWhiteSpace(InsertValue))
{
filtered.Add(ItemToRemoveOrInsert, InsertValue);
}
}
string StrQr = string.Join("&", filtered.AllKeys.Select(key => key + "=" + filtered[key]).ToArray());
if (!string.IsNullOrWhiteSpace(StrQr)){
StrQr="?" + StrQr;
}
return StrQr;
}
i have a problem passing ByteArray from flash (as3) to amfphp to save an image.
With old version of amfphp, all worked in the past… now, with new version i have many problem.
I'm using version 2.0.1 and the first problem is that i have to do this, for access to my info:
function SaveAsJPEG($json)
{
$string = json_encode($json);
$obj = json_decode($string);
$compressed = $obj->{'compressed'};
}
in the past i wrote only:
function SaveAsJPEG($json)
{
$compressed = $json['compressed'];
}
Anyway… now i can take all data (if i use " $json['compressed']" i receive an error) but i can't receive my ByteArray data.
From flash i write this:
var tempObj:Object = new Object();
tempObj["jpgStream "]= createBitStream(myBitmmapData); // return ByteArray
tempObj["compressed"] = false;
tempObj["dir"] = linkToSave;
tempObj["name"] = this.imageName;
So.. in my php class i receive all correct info, except "jpgStream" that seems "null".
Do you have any idea?
I think you get 'null' because of json_encode/decode. Try using
$data = (array) $json;
$compressed = $data['compressed'];
This may help http://www.silexlabs.org/amfphp/documentation/data-types/