I have an array
ArrayList<String> selectedItems = new ArrayList<String>();
and few strings
private String sq,tr;
am sending these values to a remote server via POST request
nameValuePair = new ArrayList<NameValuePair>(3);
nameValuePair.add(new BasicNameValuePair("sq", sq));
nameValuePair.add(new BasicNameValuePair("tr", tr));
nameValuePair.add(new BasicNameValuePair("sub[]", selectedItems);
My problem is am able to send strings but when I try to send the array am getting errors
Please suggest me the best way to send array as well as strings via post method or guide me if am making some mistake.
try to put that in loop
for (int i = 0; i < selectedItems.length; i++) {
nameValuePairs.add(new BasicNameValuePair("sub[]",selectedItems[i]));
}
Related
I am working on an android app which is retrieving information from a MySQL database.
The android app is posting to a PHP REST web service and the web service is returning JSON data.
What I am currently trying to do is get a list of databases on the MySQL server in alphabetical order. When the JSON is printed to the logcat it seems to be in the right order but when I call json.names() on the JSONObject in android it then has the databases in the wrong order.
For example, the logcat might shown it as db1, db2, db3, db4 but when the array is returned from json.names() its then appears to be in a random order. For example db4, db1, db3, db2.
Is there a particular reason for this and how can I stop this from happening.
Thanks for any help you can provide
Below is the code that processes the JSON
public void processConnectDBResult(IConnectDB iConnectDb, JSONObject json)
{
ArrayList<String> databases = new ArrayList<String>();
ArrayList<String> tables = null;
HashMap<String, List<String>> dbAndTables = new HashMap<String, List<String>>();
try
{
//Retrieve the array of the databases
JSONArray databasesJson = json.names();
for (int i = 0; i < databasesJson.length(); i++)
{
databases.add(databasesJson.getString(i));
//Retrieve the tables array from the database array
JSONArray tablesJson = json.getJSONArray(databasesJson.getString(i));
tables = new ArrayList<String>();
for (int j = 0; j < tablesJson.length(); j++)
{
tables.add(tablesJson.getString(j));
}
dbAndTables.put(databases.get(i), tables);
}
iConnectDb.processDatabaseRetrievalResult("SUCCESS", "", databases, dbAndTables);
}
catch (Exception ex)
{
}
}
i want to post from Android to a CSV File correctly. Now the php posts the Question, Answer and the User in one column. With \n i can switch to a new row in the CSV File. \t doesn't work.
Now it look like this:
Column1
Question1Answer1User1
Question2Answer2User2
...
It should view like this:
Column1| Column2| Column3
Question1| Answer1 | User1
Question2| Answer2 | User2
..
The Java Code inside the Android App.
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (int i = 0; i < dbHandler.getAllStimmungen().size(); i++) {
params.add(new BasicNameValuePair("question"+i, dbHandler.getAllStimmungen().get(i).getQuestion()));
params.add(new BasicNameValuePair("answer"+i, dbHandler.getAllStimmungen().get(i).getAnswer()));
params.add(new BasicNameValuePair("user"+i, httpHandler.getContact(1).getuserName()+"\n"));
}
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
return null;
}
The PHP Code
<?php
$timestamp = date("d.m.y-H:i:s");
$filename= 'myTextFile-'.$timestamp.'.csv';
$lastIndex = 0;
// receive data from app's http request
$data=($_POST);
// write data from my android app to a text file
file_put_contents($filename,$data);
?>
Sorry for my English!
CSV stands for Comma Separated Value. It means that columns are separated by commas. Said that, a new line character \n switches to a new row and has nothing to do with columns. You have to put commas between every single value in the csv file.
column1,column2,column3, \n
value1,value2,value3, \n
value11, value22, value33,
I'm trying to do a online card game for android with php and mysql... all working, but when I test for a long time my app the server not respond for 5 or 10 minutes for all devices in the same DNS...
I'm trying to get the refresh of SQL with a loop on AssyncTask, something like:
while(true){
try{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", Integer.toString(777)));
json = jsonParser.makeHttpRequest(url_get, "POST", params);
} catch(Exception e){
}
}
Someone can help here? i'm lost!
Thanks!
I am new to JSON. KIndly help me with the JSON parsing in php sent from android.
I have a class A, having members phoneNumber and name. I have an arrayList of object A
private ArrayList<A> contactList = new ArrayList<A>();
contactList.add(a1);
contactList.add(a2); [objects of A]
Now I am trying to send this arrayList to php server using JSON.
JSONObject json = new JSONObject();
json.put("contactList", contactList);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("");
StringEntity se = new StringEntity("JSON: " + json.toString());
post.setEntity(se);
HttpResponse resP = client.execute(post);
Please let me know how to I parse it in the php server side to get phoneNumber and name of each object A.
I tried to create a same class A in the php server side and trying this way.
<?php
$contactList = array();
if(isset($_POST["contactList"])) {
$contactList = json_decode($_POST["contactList"]);
include_once './eachContactClass.php';
foreach ($contactList->contactList as $eachContact) {
$eachObj = new eachContactClass();
$eachObj = $eachContact;
$name = $eachObj->getName();
$phoneNumber = $eachObj->getPhone();
}
}
Please let me know whether the approach is correct, or kindly help me to correct it
First of all, may I suggest you to use a library for handling the JSON serialization/deserialization. GSON would be suited for your work.
Then, you should check the result JSON for validity before sending it to any remote server.
To parse it in PHP, use the json_decode() function that will return your an object representing your JSON. You can also get a hash if you prefer, just look in the doc.
I think your problem is that your JSON is invalid, as the JSONObject doesn't correctly serialize your ArrayList. Your should probably check that.
I try to send long array from android to PHP via JSON. I did the same thing with Javascript and worked but with JAVA it gets confusing. When I send the parameters, long array list changes.
This is the part that creates the list.
JSONArray list = new JSONArray();
for (int i = 0; i < users.size(); i++) {
list.put(users.get(i).getId());
}
This is the code in JAVA that send the data.
public JSONObject sendFacebookFriendList(JSONArray list) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("list", list.toString()));
JSONParser jsonParser = new JSONParser();
JSONObject json = jsonParser.getJSONFromUrl(accountServer, params);
return json;
}
And this is the code that receives the data in PHP.
$list = $_POST['list'];
$result = array("success" => 1, "list" => $list);
While sending with Javascript the $list variable was becoming long array directly but I couldn't send it same way with JAVA.
When I send the list back to JAVA from PHP without any change I see that each array element has \" at the head and the end
So this list:
list= ["517565130","523709375","524503024","524620558","524965930", ...
becomes this:
"list":"[\"517565130\",\"523709375\",\"524503024\",\"524620558\", ...
So I cannot parse this array in PHP.
I couldn't find any way to send the long/int array in a proper way.
I appreciate if someone can fix this or suggest another way.
Thanks
I solved the problem. The thing I skipped was decoding the json array that encoded at android part. So after getting the posted data it is needed to be decoded like this;
$list = $_POST['list'];
$obj = json_decode($list);
And then I can use $obj as array.