Sending data from android to php using http - java

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(...)

Related

JAVA - How to make a purchases.products.acknowledge request

I have to make a request with the POST method as described in the guide: https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.products/acknowledge
My Java code currently looks like this:
httpClient = HttpClientBuilder.create().build();
post = new HttpPost("https://androidpublisher.googleapis.com/androidpublisher/v3/applications");
ArrayList<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("packageName", "com.my.app"));
nvps.add(new BasicNameValuePair("productId", productID));
nvps.add(new BasicNameValuePair("token", token));
post.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
response = httpClient.execute(post);
I receive a 404 web page, page not found, what am I wrong with my request?
Thanks to everyone who will try to help me. I love you <3
Without having knowledge about your HTTPClient...
I think you are using the wrong URL.
That is from the docs:
https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/products/{productId}/tokens/{token}:acknowledge
and your URL simply is:
https://androidpublisher.googleapis.com/androidpublisher/v3/applications
Besides that, i think you are posting the params for the URL as an entity in the payload. Instead you have to fill the variables in the url with it.
The only allowed entity in the payload is in the form of:
{
"developerPayload": string
}

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.

Retrieve data from Android HTTP Post

I have been looking at several other posts and what I'm trying to do is that retrieve the object over request with Android which is set.
I'm using google gcm application.
My Android the code is :
DefaultHttpClient hc=new DefaultHttpClient();
ResponseHandler <String> res=new BasicResponseHandler();
HttpPost postMethod=new HttpPost("http://192.168.1.111:8080/abc/d");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("My_Value", "My_Value_entered"));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
String response=hc.execute(postMethod,res);
I want to fetch the the value My_value in my Application(in the servlet end).
Edit: If any change or modification is needed in my android code to get the object value from Android Post,feel free to reply.
Any idea.....
You can try jsoup which is much easier to use. You can check it at jsoup.org
edit:
like this:
Document document = Jsoup.connect("http://www......com/....php")
.data("user","user","password","12345","email","info#tutorialswindow.com")
.method(Method.POST)
.execute()
.parse();

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

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.

translating curl command into java/android

Can someone help me figure out how to translate the following curl commands into Java syntax for use in an Android application?
The curl commands are:
curl -u username:password -H "Content-Type:application/vnd.org.snia.cdmi.container" http://localhost:8080/user
curl -u username:password -T /home/user1/a.jpg -H "Content-Type:image" http://localhost:8080/user/a.jpg
thanks
You can use Apache HttpClient in Android for performing HTTP posts.
HttpClient code snippet (untested)
public void postData(String url) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
// Set the headers (your -H options)
httpost.setHeader("Content-type", "application/json");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("param1", "value1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
// Exception handling
}
}
Check the following link for the basic authentication part (your -u option)
http://dlinsin.blogspot.com/2009/08/http-basic-authentication-with-android.html
Check the following answer for your file upload (your -T option)
How to upload a file using Java HttpClient library working with PHP

Categories