unable to send data from android to php - android

i was trying to upload data from my android app to php but, i am not receiving the data at the server.i tried echoing the data, can any one help with this code,i am not getting any error
nameValuePairs.add(new BasicNameValuePair("trip_id",x));
nameValuePairs.add(new BasicNameValuePair("loc_lat",String.valueOf(location.getLatitude())));
nameValuePairs.add(new BasicNameValuePair("loc_lon",String.valueOf(location.getLongitude())));
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://xx/x.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

I had a problem like this one. I'm comparing your code with mine, but I remember that the problem was the server redirecting me wasn't sending the POSTrequest. I had to type the IP of the website and use GET method.
Try using
HttpPost httppost=new HttpPost("x.x.x.x");
Even though it's not a solution (except if your server has a fixed IP), it could help finding the problem.

Related

Android HTTPS POST Give Error Not Trusted Server Certificate

I have following android POST code. It works fine for http but fails for HTTPS with Not Trusted Server Certificate exception. I do not mind self signing or accepting all certificates code (drawback less secure with man-in-the-middle attack).
HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("postValue1", "my Value"));
nameValuePairs.add(new BasicNameValuePair("postValue2", "2nd Value"));
nameValuePairs.add(new BasicNameValuePair("postValue3", "3rd Value"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient client = new DefaultHttpClient(); HttpResponse response =
client.execute(post); HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
Any help appreciated here. I need the certificate portion of the code with submitting POST with postValue1, postValue2, and postValue3 (minimum 3).
You should add the selfsigned certificate to the Android trust store. I've not worked on android self signed cert, but on windows, it will work(mostly)
I've looked up link here. The author gave good steps to install the self signed cert to android.
More discussion here

Genymotion unable to connect to server?

I have an app that sends a http request to a server and receives a JSON for processing. I test it on both a physical device and Genymotion.
The app runs fine on the physical device but on Genymotion throws NetworkOnMainThreadException.
I tracked the exception and this is the part with the issue:
..
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
// Exception on this line:
HttpResponse httpResponse = httpClient.execute(httpPost);
//
HttpEntity httpEntity = httpResponse.getEntity();
...
It seems that the Genymotion can't connect to server to execute the request. But its browser loads the sites just fine.
So, anyone knows what's going wrong here?
I found what the problem was:
As explained in this answer, since API 11, the NetworkOnMainThreadException is thrown to inform using long-running tasks (like http communications) in the main thread.
By using AsyncTask the problem was resolved and everything worked as it should.

How can i improve performance of calling web services in android?

I developed an android application using web services. Here i am calling web services to get data from server and showed in my application views. Application is working fine but calling web services is gave me performance issues. It will take more time to get data.
I am using the below code to call api and used handler to parse the data. And my result is in XML format. I am using SAX parser to parse data. I don't know why the application is very slow to get data and parse. Please provide me good performance service hint for me.
Here is my api calling code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
StringEntity entity = null;
entity = new StringEntity(xmlRequest, HTTP.UTF_8);
httppost.setHeader("Content-Type", "text/xml");
httppost.setEntity(se);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
InputStream is = httpResponse.getEntity().getContent();
I am converting this input stream to string builder and parsing.
Thanks in advance.

HTTP POST and RESPONSE specific case study

Site: http://na.leagueoflegends.com/ladders/solo-5x5
Search for a a player (example: Jaiybe)
You get redirected (in this case to: http://na.leagueoflegends.com/ladders/solo-5x5?highlight=28&page=1)
Read the content
And I want to do that in java/android.
I analyze the sites POST request when searching, result:
op:Search
player:Jaiybe
ladder_id:3
form_build_id:form-fff5e6e2569f1e15e5a5caf2a61c15e2
form_id:ladders_filter_form
Build a simple HTTP POST mixture and lets read the content...
The CODE:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://na.leagueoflegends.com/ladders/solo-5x5");
// Add your POST METHOD attributes
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("op", "Search"));
nameValuePairs.add(new BasicNameValuePair("player", Jaiybe));
nameValuePairs.add(new BasicNameValuePair("ladder_id", "3"));
nameValuePairs.add(new BasicNameValuePair("form_build_id","form-daca6fff89cedc352ccc3f533afa3804"));
nameValuePairs.add(new BasicNameValuePair("form_id","ladders_filter_form"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
responseBody = EntityUtils.toString(response.getEntity());
return responseBody;
And when I run it - I get so some kind of a offline page...
The number form_build_id - is constantly changing, but this was no problem, to use still the same one, and also If I would like to "test" if this could be the problem, I have no Idea how would I...
OR: Is there any other - FAST - way how to get same results?
What is strange is that the "error" site source code that I get on android is different as if I run the same on my PC (Win7, Eclipse, Java) or in my browser. As if there would be two versions of offline sites - for mobile and for PC - but my question: HOW WOULD the server know that the code runs on a Android device? Is there a way how to set this up in HttpClient?
form_build_id:form-fff5e6e2569f1e15e5a5caf2a61c15e2
This is an auto generated token that is valid for a certain time period. This is probably the source of your problem and the reason the token exists in the first place (to prevent post spams).
As this token does not seem session based, you could actually use an HTTP Get on the page that generates the form and parse out the generated token each time for your HTTP Post.
About OS detection, browsers usually provide information about the OS using the HTTP User-Agent header.

How to get a response from a https server using android sdk?

I have a problem of ssl exception when i upload data to a https server. It uploaded the data to the server correctly but when i get the response after uploading it throws an exception of ssl certificate is not trusted. I'm using the SAX parser for parsing xml file and i am using httppost method().
you have to add a new scheme to accept Secure site connections
check this, and there you will find another useful sample without checking the cetificate...
Https Connection Android
Android comes with the apache commons http library included. Setting up a https post request is quite easy:
HttpPost post = new HttpPost("https://yourdomain.com/yourskript.xyz");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("postValue1", "my Value"));
nameValuePairs.add(new BasicNameValuePair("postValue2", "2nd Value"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
Android uses a version 4.x of the commons http library as all versions below 4.0 are out of their lifecycle.
I can't tell exactly how to register a self-signed certificate to the HttpClient, but mybe the commons http documentation helps:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506

Categories

Resources