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!
Related
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]));
}
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 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 am trying to post a Date object Date date = new Date() to a remote PHP script using HTTPClient but it seems like NameValuePair does not accept any other Java objects other than a String? Would appreciate if you can guide me how to post a Date object using HTTPClient
Here's my code
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
Date date = new Date();
postParameters.add(new BasicNameValuePair("stringObj", "Test")); //No error
postParameters.add(new BasicNameValuePair("dateTime", date)); //Error here
try{
String response = CustomHttpClient.executeHttpPost("http://remotewebsite/test.php", postParameters);
catch{
// ...
}
Cast it to string. Make sure date is not empty/null (I didn't see in your code that you used the date object with some of the methods from the Date class)
For example:
postParameters.add(new BasicNameValuePair("dateTime", new Long(date.getTime()).toString()));