In my application I'm using outpost method to send data to web service which is in .net.Now when i send string with spaces or special characters it takes the string with e.g. test string would display like test+string.
I'm using httpDefaultClient with nameValuePair to send data...i've used UrlEncode function to encode my string but still result is the same...
Please help me...
Here is my code
//web service call
HttpClient client=new DefaultHttpClient();
HttpPost request = new HttpPost();
request.setURI(new URI(url2));
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
// nameValuePairs.add(new BasicNameValuePair("CreateHotSpots", value))
nameValuePairs.add(new BasicNameValuePair("sKey",""+globalClass.getUser_key()));
nameValuePairs.add(new BasicNameValuePair("sLAKE",encodedlakename));
if(!(DragAndDropPinActivity.point.isEmpty())){
nameValuePairs.add(new BasicNameValuePair("sLAT",""+DragAndDropPinActivity.point.get(0)));
nameValuePairs.add(new BasicNameValuePair("sLon",""+DragAndDropPinActivity.point.get(1)));
}
else
{
url2=url2.concat("&sLAT="+""+myLatitude);
url2=url2.concat("&sLon="+""+myLongitude);
}
nameValuePairs.add(new BasicNameValuePair("sDesc",encodedDesc));
nameValuePairs.add(new BasicNameValuePair("sSpeciesofFish",encodedFishSpecies));
nameValuePairs.add(new BasicNameValuePair("sBaitUsed",encodedbUsed));
nameValuePairs.add(new BasicNameValuePair("sWeatherInformation",encodedwInfo));
nameValuePairs.add(new BasicNameValuePair("season",encodedlseason));
nameValuePairs.add(new BasicNameValuePair("sDesc",""+encodedDesc));
if(share)
{
nameValuePairs.add(new BasicNameValuePair("sShareInfo","true"));
}
else
{
nameValuePairs.add(new BasicNameValuePair("sShareInfo","false"));
}
Log.e("name value pairs",""+nameValuePairs.toString());
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
request.setEntity(entity_st);
HttpResponse response = client.execute(request);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Responce=EntityUtils.toString(resEntity);
Log.i("responce ======",""+Responce);
}
}
catch (Exception e) {
e.printStackTrace();
String message=e.getMessage();
Log.e("meaasge with erroe",message);
}
return Responce;
}
step 1
You have specified a "UTF-8" encoding in
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
Try to change it to
UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs);
step 2
I noticed that the strings you are adding to the request are called encodeXXXX
Does this mean that you are encoding them before adding them to the ValuePairs?
If so, stop doing this and keep them as normal strings.
Related
I'm new in Android development. I made an Android app for login in a website. The login page takes three inputs 'username', 'password' & 'pin'.
I've successfully passed these data using HttpPost method. See the code,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mysite.com/api/service.php");
try {
// Add user name, password & pin
String action = "login";
EditText uname = (EditText)findViewById(R.id.txt_username);
String username = uname.getText().toString();
EditText pword = (EditText)findViewById(R.id.txt_password);
String password = pword.getText().toString();
EditText pcode = (EditText) findViewById (R.id.txt_pin);
String pin = pcode.getText().toString();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("action", action));
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("pin", pin));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("PS", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Now i want to parse the 'ack' & 'msg' from HttpResponse which is a XML output appears after passing data to the website. See here,
<login>
<member>
<id/>
<username/>
<name/>
<ewallpoints/>
<ack>FAILED</ack>
<msg>Wrong Username and Password</msg>
</member>
</login>
Use response.getEntity().getContent() to get an input stream and process it with DOM, SAX or XPath as #tobias suggested. There are many options.
You may also get the xml string directly. Change the line
HttpResponse response = httpclient.execute(httppost);
to
String xml = httpclient.execute(httppost, new BasicResponseHandler());
How do I send the data to this PHP file below? I can establish the connection and send data, but can't receive the response. I need to send 3 parameters, the "op", Username and password.
switch ($_POST["op"]) {
// User Authentication.
case 1:
$UName = $_POST["UName"];
$UPass = $_POST["UPass"];
$UPass = md5($UPass);
//....Some code
// New user registration process.
case 2:
$UName = $_POST["UName"];
$UPass = $_POST["UPass"];
$UEmail = $_POST["UEmail"];
$UNick = $_POST["UNick"];
$UPass = md5($UPass);
//....Some code
}
My code so far:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://SomeUrl/login.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("1", "dan"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());
Log.d(TAG, ""+responseBody);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
What am I doing wrong?
Looking at your PHP code, at a bare minimum you need to specify an op parameter. Then depending on which op you're trying to carry out, make sure you specify the other needed parameters as well. For user authentication (op = 1):
nameValuePairs.add(new BasicNameValuePair("op", "1"));
nameValuePairs.add(new BasicNameValuePair("UName", "dan"));
nameValuePairs.add(new BasicNameValuePair("Pass", "somepass"));
On a side note, you should secure the PHP service with SSL, if you are using it to process sensitive user information.
Android HTTP POST should look like this:
HttpParams httpParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 10000);
// Data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("UName", "dan"));
nameValuePairs.add(new BasicNameValuePair("UPass", "password"));
nameValuePairs.add(new BasicNameValuePair("op", "1"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://SomeUrl/login.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
}
In your PHP code I would strongly recommend to use mysql_real_escape_string($_POST['UName']) otherwise it is easy to attack via SQL injection.
I would also recommend to use either SSL connection (HTTPS) or to send the password only hashed (MD5)
blahNow I am doing an ANdroid application.In my app I have to login using a url.Just like...www.blah.com/api/login/username/password.
private void sendAccelerationData()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7);
nameValuePairs.add(new BasicNameValuePair("","test3"));
nameValuePairs.add(new BasicNameValuePair("","pass3"));
this.sendData(nameValuePairs);
}
private void sendData(ArrayList<NameValuePair> data)
{
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://blah.com/api/login/");
httppost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse response = httpclient.execute(httppost);
}
}
but I am getting 404 error.and if i write my sendData() like
HttpPost("http://eesnap.com");
and sendAccelerationData() like
nameValuePairs.add(new BasicNameValuePair("","api"));
nameValuePairs.add(new BasicNameValuePair("","login"));
nameValuePairs.add(new BasicNameValuePair("","test3"));
nameValuePairs.add(new BasicNameValuePair("","pass3"));
I am getting 200 success.
If post www.blah.com/api/login/username/password on brower then I am getting a result on the browser
Try:
HttpPost("http://www.blah.com/api/login/");
I understand my error.In 'Get()' method i can write code like above but in post method I have to use name value pair.
I'm trying to do a post to a url by sending a username and a password using the post method.But the answer is unclear.
Here is how I do it:
HttpClient httpclient =new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://test.res-novae.fr/xperia_orange/scripts/android_ckeck_user.php");
try{
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));
HttpResponse response = httpclient.execute(httppost);
/*Checking response*/
if(response!=null)
{
InputStream in = response.getEntity().getContent();
System.out.println("Resultat de la server:"+in);
}
}
catch(ClientProtocolException e)
{
}
catch (IOException e) {
// TODO Auto-generated catch block
}
and this is the answer from server:
Resultat de la server:org.apache.http.conn.EofSensorInputStream#44e067e0
Does anyone know what I'm doing wrong?
You are not reading the stream, but are rather looking at a String representation of the underlying InputStream object.
You will want to read the contents of the InputStream. Take a look at this question for examples of how to do this.
I am trying to retrieve a response from my server which ultimately redirects to a URL after post data is submitted. I figured I could retrieve the url response that the server redirects with , with the code below but every time I do i get a fatal error out of memory. I was wondering what may cause this error if any one had any ideas of why I am getting this error or if this is an incorrect way? I have tested I am sending the data correctly to the server but keep getting this error. here is my code.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("token", token));
nameValuePairs.add(new BasicNameValuePair("key", "test"));
nameValuePairs.add(new BasicNameValuePair("mac", "test"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
ResponseHandler<String> responseHandler=new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
Log.d("URL", responseBody);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}