Making comments on a wordpress.org blog on android - java

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.

Related

Android login and redirect

I am trying to get some data from a webpage that needs login in a page. I could do with htmlunit but gave me problems import these libraries on Android. So I am trying to do it with apache http client.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://inside.cineca.it/cgi-bin/uinside/marcature.pl");
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("j_username", "user");
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("j_password", "pass");
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(EntityUtils.toString(httpResponse.getEntity()));
But is giving me error. I am affraid i should manage the redirect but i don't know how. I would be happy if someone could give me some advice.
Best regards
You're performing an http POST to a login page. Maybe the server that serves you back the login page doesn't know to handle the post. Anyway, I am sure your intention is to perform the login actually. You need to figure out what is the server API where you should post the credentials and process the result. Performing a quick investigation with Chrome Network profiler, the login API for above link actually resides at: https://idp-is.cineca.it/idp/Authn/Multilogin. That is the URL that you should be using and there is where you need to POST.
Also, make sure the request you're making is done on a non-UI thread as this is a common mistake done by beginners.

how to set myself URL when make call by Twilio?

As you know, use Twilio to make a call, needs pass a URL tell Twilio to fetch instructions.
And I deploy my URL on http://1xx.16.102.129:8080/voice/voice.xml
Here is my code:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Url","http://1xx.16.102.129:8080/voice/voice.xml"));
params.add(new BasicNameValuePair("To", "+1xxxxxxxxxxxx"));
params.add(new BasicNameValuePair("From", "+15086895110"));
CallFactory callFactory = client.getAccount().getCallFactory();
Call call = callFactory.create(params);
After the phone call end, I got exception like "502 Bad Gatewayā€¯". Can anyone tell me what is the problem?
Twilio evangelist here.
502 Bad Gateway is usually an indication that Twilio tried to request content from the URL you specified when initiating the outbound call but had trouble.
There are a lot of things that can cause this including things like problems resolving DNS, not including a Content-Type header in the response or network disruptions between Twilio and your server.
We have put together a number of troubleshooting tips that you can find on our website that you can use to help diagnose the problem.
Hope that helps.

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

Multiple Difficulties with HttpURLConnection class in Java for Android

--Update--
Apologies for those who helped me, it turns out this is just a problem with Eclipse's debugger. After suspecting that it was leading me wrong, I placed down a couple of System.out.println to watch the variables, and according to them they ARE being changed, and that the debugger was just showing me old information for whatever reason. No clue why that's happening, but the important thing is that the code does apparently actually work.
I'm working on a method to share with twitter for an Android application, and I'm having errors when setting up the HttpURLConnection. I create the connection object as per usual, using the openconnection function of a url then casting it to a HttpURLConnection, and when I subsequently run SetRequestMethod("POST") on the connection, it does absolutely nothing. When I run the code in the debugger line by line, as I go through that line the request method just remains as the default ("GET"). Anyone have any idea as to why this may be happening? I'm getting the same problem with setDoOutput(true) also not changing anything. However, adding a request property does still work. I've been searching around and haven't been able to find anything on this problem, not even another person reporting these problems.
I am not sur whether using HttpURLConnection is the best here.
Did you try the following way?
// Building the POST request
final BasicNameValuePair message = new BasicNameValuePair("yourField", "yourContent");
final List<NameValuePair> list = new ArrayList<NameValuePair>(1);
list.add(message);
final HttpPost httppost = createHttpPost(UrlEncodedFormEntity(list));
// Building the HTTP client
final HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, YOUR_CHOSEN_CONN_TIMEOUT);
HttpConnectionParams.setSoTimeout (httpParameters, YOUR_CHOSEN_SO_TIMEOUT);
final HttpClient httpClient = new DefaultHttpClient(httpParameters);
// Execution of the POST request
final HttpResponse response = httpClient.execute(httppost);
This is the way I usually do, with no problems.
[EDIT: 04-25-2014] Apache's HttpClient was the best approach for Froyo and former versions. Now, according to this article from Android Developers Blog (written after this Q&A), it is better to use URLConnection.

Django-Piston and Python Client Vs Java Client

I've built a web service using Django-Piston that allows for POST and GET requests. As part of testing I wrote a quick Python script. Using the script I can successfully do both types of requests; however, when a client written in Java attempts to do a POST I get an error: "POST /api/service/ HTTP/1.1" 400 225 "-" "Apache-HttpClient/4.1 (java 1.5)"
My understanding is that http request message that is generated by any language has to be the same. In other words, if I test my web service using a python client and it works then it should work for all other languages that have a http library.
Here is the python code for the POST:
import urllib, urllib2
data = urllib.urlencode({'url': 'www.uvic.ca', 'name': 'uvic'})
url = 'http://xxx/api/service/'
req = urllib2.Request(url, data)
print urllib2.urlopen(req).read()
and here is the Java code:
HttpPost httpost = new HttpPost("http://xxx/api/service/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "some_name"));
nvps.add(new BasicNameValuePair("url", "www.somename.com"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println(entity.getContentType());
System.out.println(EntityUtils.getContentCharSet(entity));
System.out.println(EntityUtils.toString(entity));
I'm starting to think that this is an apache configuration problem. I put in some debugging statements at the start of my method for POST and I'm not hitting them at all. This means that something is wrong with the urls.py file (which I doubt because it works in python) or something is weird with apache.
Please help. Thanks in advance.
A little search would help you a lot. This was the first Google result.
http://weblog.mattdorn.com/content/restful-web-apps-with-django-piston-and-ext-js/
The reason for the 400 errors is that
ExtJS is appending a charset onto the
Content-Type field, which causes
piston to not interpret the
Content-Type correcly. There is an
open issue for it at
http://bitbucket.org/jespern/django-piston/issue/121/content-type-is-not-being-split-against.
I was able to get the example working
after I applied the patch and did an
easy_install.
This is the second Google response.
https://bitbucket.org/jespern/django-piston/issue/99/bad-request-and-content-type-with-fix

Categories