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();
Related
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
}
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.
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(...)
I am trying add a feature to add comments in my app for my blog. I was trying to achieve this through httpclient, but i am continuously failing to achieve this. Here is my code:
HttpClient client = new DefaultHttpClient();
HttpPost http = new HttpPost("http://universityoftrollogy.wordpress.com/wp-comments-post.php");
http.setHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("_wp_http_referer", referer));
nameValuePairs.add(new BasicNameValuePair("hc_post_as", "guest"));
nameValuePairs.add(new BasicNameValuePair("comment_post_ID", postId));
nameValuePairs.add(new BasicNameValuePair("comment_parent","0"));
nameValuePairs.add(new BasicNameValuePair("comment", cData));
nameValuePairs.add(new BasicNameValuePair("email", cEmail));
nameValuePairs.add(new BasicNameValuePair("author", cName));
http.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(http);
My client executes properly but I cannot see any comments on my post!
I am not sure why isn't it working but a possible reason may be that it is not handling redirects.
Can anyone help me by guiding me to the right way to achieve it? Any help would be greatly appreciated.
Edit : After checking my response, I found that the status line for response is 500.
When you work against a wordpress.com blog, you will move from one bug to the next,
without being able to diagnose the errors.
So my advise: Set up your own webserver and wordpress-blog on your development machine. It is not difficult.
Then you can check the error-log of your webserver what wordpress did not like.
And yes, you probably need a "nonce". You can read about in in the codex-page. But remember that the codex-page is written from the perspective of a wordpress-plugin-developer.
there is a parameter called xxx_none to validate user in wordpress. It is generated randomly, I think, when a client open a session.
you may need to try to open the page once, then parse that parameter (I think in your page, it is "highlander_comment_nonce"), then put it into the http request too?
Tip: if you are using chrome, there is tool in the developer tool which help you to copy the request in the cUrl format, so you can see all the parameters sent to server.
I found so many samples for requesting a REST API, but all together are confusing, can some one please explain me a way to use http requests.
My Requirement is, I want to get data from a REST API by providing username, pwd and a key.
What I have Used was,
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("REST API url");
post.setHeader("Content-type", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "un");
obj.put("pwd", "password");
obj.put("key","123456");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
But the response is always null and these working fine when tested with browser tool by posting the same data.Is some thing wrong with my approach? please suggest me the correct way. Thank you
(1) Google I/O video session for developing REST clients
(2) search in android developer blog
(3) https://github.com/darko1002001/android-rest-client
Please try after that post your question,
I can share code snippet from my rest client developed based on (1) & (2)
Do not use Cloud to Device Messaging, instead use the latest cloud approach with android application development.
There is new library called Volley, which looks better than AsyncTask. It should be useful in developing RESTful clients.
You probably forgot to add the internet permission to the manifest file.
Add the following line.
<uses-permission android:name="android.permission.INTERNET" />
I thing you should try this,
HttpContext localContext = new BasicHttpContext();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("REST API url");
post.setHeader("Content-type", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "un");
obj.put("pwd", "password");
obj.put("key","123456");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post,localContext);
Hope this will help.
By any chance, is the server expecting a GET request for this operation? If so, you may want to use HttpGet instead of HttpPost.