How to send a string to a php file from Android? - java

I have a php script on a local server that creates and manages an SQL table. I am able to create the table and database through my android app but I am having trouble figuring out how to send data to the php file. I want to send a string so that I can sort and pick the values to return to my app.
How do I change my php and android code so that I can get entries in the table between 2 dates?
Here is some of my Android code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.0.3/xampp/information.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
The beginning of information.php script is setup like this:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("data",$con);
How do I send a string from Android to the php file?

You should try using $_POST variables. So your HttpPost object would be initialized like this:
HttpPost httppost = new HttpPost("http://10.0.0.3/xampp/information.php?info="+nameValuePairs);
Then, in your PHP, just check to see if the variable info is set, and then process it if it is.
if(isset(_$POST['info']))
//process data
This would, of course, requiring some formatting of nameValuePairs so that it is a valid URL, but it forces everything into one variable which you can easily check in your PHP.

Related

How to send POST request using HTTPURLConnection with JSON data

I have a RESTful API that I can call by doing the following:
curl -H "Content-Type: application/json" -d '{"url":"http://www.example.com"}' http://www.example.com/post
In Java, when I print out the received request data from the cURL, I correctly get the following data:
Log: Data grabbed for POST data: {"url":"http://www.example.com/url"}
But when I send a POST request via Java using HttpClient/HttpPost, I am getting poorly formatted data that does not allow me to grab the key-value from the JSON.
Log: Data grabbed for POST data: url=http%3A%2F%2Fwww.example.com%2Furl
In Java, I am doing this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/post/");
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
BasicNameValuePair nvp1 = new BasicNameValuePair("url", "http://www.example.com/url);
nameValuePairs.add(nvp1);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpresponse = httpclient.execute(httppost);
How do I make it so that the request from Java is similar to cURL in terms of how the data is sent?
The data you present as coming from the Java client are URL-encoded. You appear to specifically request that by using a UrlEncodedFormEntity. It is not essential for the body of a POST request to be URL-encoded, so if you don't want that then use a more appropriate HttpEntity implementation.
In fact, if you want to convert generic name/value pairs to a JSON-format request body, as it seems you do, then you probably need either to use a JSON-specific HttpEntity implementation or to use a plainer implementation that allows you to format the body directly.

read url content WHILE it is continuously updating

I have a php script that makes a few api calls in a row and once an api call has
returned data the script outputs the content. Meaning if you call the script on
the browser you will wait 1 second then see some content appear, than after 2 seconds
more content will be appended to the page and so on.
The thing is I am accessing this content from java/android in one of my apps.
Is there a way I can read this content from java WHILE it is updating? This
way I will populate the application content as new data is being fetched from
the script.
I have tried something like this when I have accessed xml files but they
were not continuously updating.
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
}
Yes, use URLConnection instead of HttpClient. Here's a tutorial.
Like in the tutorial, connection.getInputStream() will return a stream you can read and process, for example line-wise.

How to send strings to server using Java?

I need to send a lot of strings to a web server using Java.
I have a List<String> with huge amount of strings and I need to send it via POST request to the Struts2 action on the server side.
I have tried something starting with
HttpPost httppost = new HttpPost(urlStr);
but don't know how to use it.
On other side I have a Struts2 action, and getting the POST request is easy to me.
I think this solution is too close, but it doesn't solve my problem because it's using just one string :
HTTP POST using JSON in Java
So, how to send many strings to a server using Java?
You should do somthing
HttpPost httppost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<>();
for(String s : list)
params.add(new BasicNameValuePair("param", s));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
on the other side is an action mapped to the url has setter for param. It should be
List<String> or String[]. The action when intercepted will populate that param property.

Sending data from android to php using http

This my android code I used to send to my php file in website. also modified android manifest file.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
nameValuePairs.add(new BasicNameValuePair("number", "5556"));
nameValuePairs.add(new BasicNameValuePair("password",password));
nameValuePairs.add(new BasicNameValuePair("token",token));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://futuretime.in/post.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
This is my php script for receiving data from android.
But my problem is when I printed data in php it is not showing any value.
<?php
$number= $_POST['number'];
$password = $_POST['password'];
$token = $_POST['token'];
echo $number;
?>
you can try use Ion https://github.com/koush/ion ,is a great library for make http request.
is a simple example .check the project site for more examples and wiki.
Ion.with(getContext(), "https://koush.clockworkmod.com/test/echo")
.setBodyParameter("goop", "noop")
.setBodyParameter("foo", "bar")
.asString()
.setCallback(...)

Apache Httppost retrieve image from server Java

Right now I am using Httppost to Post some parameters in the form of xml to a server. When the post occurs, a geotiff or .tif file is downloaded. I have successfully posted the document to the server and successfully downloaded the file simply by attaching the parameters to the url but I can't seem to combine the two. I have to use post because just using the URL leaves out elevation data in the geotiff.
In short, I am not sure how to simultaneously post and retrieve the image of the post. This is what I have thus far...
// Get target URL
String strURL = POST;
// Get file to be posted
String strXMLFilename = XML_PATH;
File input = new File(strXMLFilename);
// Prepare HTTP post
HttpPost post = new HttpPost(strURL);
post.setEntity(new InputStreamEntity(
new FileInputStream(input), input.length()));
// Specify content type and encoding
post.setHeader(
"Content-type", "text/xml");
// Get HTTP client
HttpClient httpclient = new DefaultHttpClient();
//Locate file to store data in
FileEntity entity = new FileEntity(newTiffFile, ContentType.create("image/geotiff"));
post.setEntity(entity);
// Execute request
try {
System.out.println("Connecting to Metoc site...\n");
HttpResponse result = httpclient.execute(post);
I was under the impression that the entity would contain the resulting image. Any help is much appreciated!
Thanks for the help guys. The entity was what was being sent to the server. I had code that was trying to read it from the response as well but it wasn't working because setting the entity to a file entity messed up the post request. By removing that part, it works great!

Categories