I would like to send an array of arrays from an android phone to a server. Is there any possibility to do this?
I expect something like this at the server side:
$_POST['items'] = array(
array('name'=>'joe', 'email'=>'joe#example.com'),
array('name'=>'jane', 'email'=>'jane#example.com')
);
Thx for your help!
Use HttpPost to send data to the server, and use json to create an array of array's. nameValuePairs will carry the data:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("items", jsonObject));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Hope this helps...
Related
I have a code to make an httpPost, but i can't log using the parameters in the array, but if i make all the complete url i will log with no problem !, what am i missing ?
String URL = "http://domain.com/projexct/form.aspx"; //won't work
// String URL = "http://domain.com/projexct/form.aspx?user=aa&pass=11";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", "aa"));
nameValuePairs.add(new BasicNameValuePair("pass", "11"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
EDIT1
It doesn't throw and error, but if i use the parames in namevaluePairs it shows just the login page, if i use the URL commented it will actualy log in.
I am sending the string value to the php file on the server like this:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("serverurl");
Log.d("httppost..", httppost + "");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", dataCount + ""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
But, I am not seeing any data which I have sent from the app.
Thanks for any help.
I need to access data from a webpage using several different post requests. For now I use:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://myurl");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "search"));
nameValuePairs.add(new BasicNameValuePair("ndc", ndc));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
I need to sent this request using different values for the variable ndc. Would looping this lines be a good idea? If so, how to reuse the HttpClient and HttpPost variables?
If the URL needs to stay the same, then you should only change the values that need to sent.
for (int i=0; i<ndcArray.length;i++)
{
if(i==theNumberWhenURLhasToBeChanged) //adjust this condition based on your knowledge when the url has to be changed, lets say if i > theNumberWhenURLhasToBeChanged, then change the url...
{
httppost = new HttpPost(URLs[theNumberWhenURLhasToBeChanged]);
}
nameValuePairs.add(new BasicNameValuePair("ndc", ndcArray[i]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
Note that: response will change each time, so bear in mind that you should save the response somewhere. And ndcArray[] can be replaced with any structure you want.
I am parsing the data from an XML file and storing it in database. Now i want to update the data through an XML file, I have an url for updating the data but i am not getting the way to send the data on that url..
pls help.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("latitude", "00.11"));
nameValuePairs.add(new BasicNameValuePair("longitude", "00.11"));
String url = "http://10.15.66.101:8080/LocationServer/GetLocation";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
BusinessManager.getHandler().getLoggerUtilityObj().printMsg(
"Posting data to server");
// This is done to shutdown the previously open http connection
httpClient.getConnectionManager().shutdown();
I am executing the following post in Android:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("account", "login"));
nameValuePairs.add(new BasicNameValuePair("email", "email#email.com));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
In the server side I return the cgi.request_method variable and it is GET.
Shouldn't it be POST? Am I missing something?
That code should initiate a POST, not a GET. I would guess that something on the server side is misconfigured.
My problem was send http instead of https