How to post string to server using BasicNameValuePair - android

I'm trying to post combination of string to my backend server. How can I achieve that using BasicNameValuePair. Here are some code which I was trying:
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpClient = new DefaultHttpClient(params);
HttpPost post = new HttpPost("API HERE");
List<NameValuePair> postData = new ArrayList<NameValuePair>();
postData.add(new BasicNameValuePair("username", "the_username"));
postData.add(new BasicNameValuePair("password", "the_password"));
I want to send username and password like:
username=USER&password=PWD
How to achieve the successful post to the server.
Help will be appreciated.

The following is what I use for all of my POST variables:
HttpParams httpParams = new BasicHttpParams();
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost;
httpPost = new HttpPost(Net_URL + Net_Get);
httpPost.setEntity(new UrlEncodedFormEntity(/*Name Value Pairs*/));
HttpResponse response = httpClient.execute(httpPost);
As a side suggestion, I would first do an some sort of encryption of a users password before sending it over the network, I have seen many who do not do this ^.^

Related

Http GET Request Android

I know that it's possible to put some data in GET request body, but I know that it's not a good practice.
Though I want to do it but i don't know how. I searched the web but couldn't find anything.
Can you give me some little code examples or reference me to some usefull info.
Let's say we have this code, how to add nameValuePairs to request body of GET request :
String url = "something";
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpGet httpGet = new HttpGet(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("dtype", "0"));
nameValuePairs.add(new BasicNameValuePair("category_id", ""));
HttpResponse response = httpClient.execute(httpGet);
System.out.println("log in");
Thanks in advance.

Sending the data to the server using HttpPost Request in android

I am sending the string value to the php file on the server like this:
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("serverurl");
Log.d("httppost..", httppost + "");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", dataCount + ""));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
But, I am not seeing any data which I have sent from the app.
Thanks for any help.

posting the data using httpput in android

I have a problem in posting the data to server by using httpput methods in android.I have to send feedback to server and getting json response. but i am getting 404 bad request. but i dont know where is the problem.
I am strucked here and didn't find any solution. Any suggestions?
My code is as follows:
HttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut(getString(R.string.feedBack));
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("userId", "8"));
pairs.add(new BasicNameValuePair("feedback",feedbackMessage
.getText().toString()));
put.addHeader("Content-Type", "application/json");
put.addHeader("Accept", "application/json");
put.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = client.execute(put);
Log.d(tag, "Result" + response.getStatusLine());
You are not initializing a URL object and passing in simple String.
You should do this instead:
URL url = new URL(getString(R.string.feedBack));
HttpClient client = new DefaultHttpClient();
HttpPut put= new HttpPut(url);
And it should work.

Android HttpPost - Send request in cp1251

I send request with this code:
final DefaultHttpClient client = new DefaultHttpClient();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("number", pNumber));
final HttpPost httpPost = new HttpPost(URL_MAIN);
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
client.execute(httpPost);
It sends request in UTF-8, but I need cp1251 (site works only with this CP). How to encode it into the cp1251?

tumblr authentication problem

I am trying to build a tumblr client in android but am running into response code 403 - Forbidden.
The api docs suggest i use email and password parameters to validate credentials and get account information.
I have tried out the following code but as specified get a 403 response code.
can anyone suggest what i m doing wrong.
String url = "http://www.tumblr.com/api/authenticate";
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 20000);
HttpConnectionParams.setSoTimeout(params, 20000);
DefaultHttpClient httpclient = new DefaultHttpClient(params);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(_username + ":" + _password);
httpclient.getCredentialsProvider().setCredentials(new AuthScope(null,-123),credentials);
HttpPost method = new HttpPost(url);
HttpResponse response = httpclient.execute(method);
response.getStatusLine().toString(); // this gives me a 403 res code (Forbidden)..
Here's what i use in my app to authenticate:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.tumblr.com/api/authenticate");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", Username));
nameValuePairs.add(new BasicNameValuePair("password", Password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() != 200) {
return false;
}
return true;
}

Categories

Resources