I am new to android and I have a question about name value pairs that I am a little confused on. For example I am trying to post to the following example and get the response code using the endpoint:
/account/create/bank-id?ssn=SSN&name=NAME&email=EMAIL. I would edit the need to edit the following : bank-id , SSN , NAME, and EMAIL. at the moment. I am thinking along the lines of(so far no luck , no repsponse code are printing so I dont know what I am doing wrong at the moment, ty for any replies):
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xx.xxx.xxx/account/create/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("bank-id", "12345"));
nameValuePairs.add(new BasicNameValuePair("ssn", "451"));
nameValuePairs.add(new BasicNameValuePair("name", "kitty"));
nameValuePairs.add(new BasicNameValuePair("name", "kitty"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.d("Http Post Response:", response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Related
The website has a counter reputation.Im have two links. First link increment reputation counter, second decrement reputation counter. How im can go to the link without opening a browser to increase or decrease the reputation counter.
You will need to post to the link.
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
You probably wont need the name value pairs to have anything so you can probably delete everything in between the Add your data and Execute HTTP Post Request comments but you will need to change the path when creating the HttpPost.
Code taken from here
I have a backend for my android app, which returns 404 on GET and json on POST. Now, I'm trying to do POST request using this snippet:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/api/login");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", "email#email.com"));
nameValuePairs.add(new BasicNameValuePair("password", "qwerty"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Server however receives GET request. With curl POST backend returns json as expected. But somehow httpPost sends GET(!) request. What could be the problem? What am I doing wrong?
Ok guys, answered my own question pretty quickly, may be helpful for others.
I replaced hostname with ip address, and it worked!
I am attempting to help a student write an android app that will contact a specific webserver. From the looks of the website, you can issue a GET requestion with your web browser and you get back a cookie session and an "authenticity token" (see the source of the page as an invisible input)
We issue a GET request and then want to follow it up with the post, but we are receiving a status code of 404 on the post. On a side note, the first GET request returns a code of 200.
Does anyone have any ideas? Below is the code that gets executed...
public void run()
{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("https://co22.herokuapp.com/login/");
HttpPost httpPost = new HttpPost("https://co22.herokuapp.com/login/sessions");
HttpResponse response;
try
{
response = httpClient.execute(httpGet);
Log.d("matt",response.getStatusLine().toString());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("session[network_id]", username));
nameValuePairs.add(new BasicNameValuePair("session[password]", password));
nameValuePairs.add(new BasicNameValuePair("authenticity_token","9yvxPOUpRFdsTeHAZtISEfBHpElDTHzvMjAbQnxOHDM="));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);
Log.d("matt",response.getStatusLine().toString());
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It appears that the above will work correctly, but we were just sending the second post request to the wrong website.
the code below has two lines that were suggested to solve the problem where fiddler cannot see a post taking place in the AVD emulator. Without the two lines the post is successful but fiddler cannot see it. With the two lines the post comes back after about 10 minutes with an I/O exception.
public HttpResponse postData()
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/api/GETTrafficDirector");
HttpResponse response = null;
//do these two lines so fiddler can see post when debugging
HttpHost proxy = new HttpHost("192.168.2.8", 8888);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
}
catch (IOException e)
{
// TODO Auto-generated catch block
}
return response;
I have android app . i should pass the data from android to web server with data security.Can any one suggest me to What are all the data security's can Provide.
You can send the data using HttpPost methos .
This is a simple example for it .
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Use Https , you should buy a SSL for your server first , and then Use https to send data.