Unable to authenticate services - android

i need to authenticate my services with username and passwor i have try this but not get sucess can you help me
when i print inlog cat i got ::
Update 3 ::
11-04 16:07:58.767: INFO/System.out(1531): Returned ======>>> <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was unable to process request. ---> Data at the root level is invalid. Line 1, position 1.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>
Code ::
DefaultHttpClient UserIDclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
nvps.add(new BasicNameValuePair("username","password"));
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = UserIDclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
Returned = EntityUtils.toString(resEntity);
System.out.println("ans is :: "+ Returned);
System.out.println("Returned ======>>> "+Returned);

1st Thing:
Check whether the "returned" string is null or not.
2nd Thing:
One thing i notice that you have written below lines for twice, what is the need to call execute() method before passing username and password:
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
Returned = EntityUtils.toString(entity);
Instead your code should be:
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Your DATA
nameValuePairs.add(new BasicNameValuePair("ID", "username"));
nameValuePairs.add(new BasicNameValuePair("Password", "password"));
response.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
HttpResponse response1 = httpclient.execute(post);
if(response1.getStatusLine().getStatusCode()==200)
{
HttpEntity resEntity = response1.getEntity();
System.out.println("=========> Response => "+iStream_to_String(resEntitiy.getContent()));
}
// You can get iStream_to_String from here.

Related

How to send Japanese characters using HttpPost

I'm trying to send Japanese characters to my API server but the characters sent were garbled and became ????. So I set the encoding to the entity using:
StringEntity stringEntity = new StringEntity(message, "UTF-8");
but the output became org.apache.http.entity.StringEntity#4316f850. I wonder if converting the stringEntity to string caused this since I want to send it in my server as String.
Here's how I used it:
public static String postSendMessage(String path, String message) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000); // Timeout limit
HttpPost httpPost = new HttpPost(SystemInfo.getApiUrl() + path);
List<NameValuePair> value = new ArrayList<NameValuePair>();
StringEntity stringEntity = new StringEntity(message, "UTF-8");
value.add(new BasicNameValuePair("message", stringEntity.toString())); //Here's where I converted the stringEntity to string
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
String result = convertStreamToString(is);
return result;
}
Where could I have gone wrong?
You don't need to use StringEntity.
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("message", message));
Instead, you have to pass a second argument for initializing `UrlEncodedFormEntity.
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value, "UTF-8");

Get the authentication from the moodle for a android application

I want to get the authentication from moodle (say the moodle site from our university) in my android application. And I do not have the access to the database. I tried to make a http post request from my application with username and password.
But when I execute the post request, I get an exception. This is what I have tried
private URI url = new URI("https://online.mrt.ac.lk/login/index.php");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "user"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
I get a exception like this
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Please someone help me.

Post JsonArray and string parameters using HttpPost in android

I tried lots of ways to send data using HttpPost but I haven't succeeded, if anybody knows about this please help?
My service url is :
http://xxxxx/xxx/abc.php?id=xx&name=xxx&data=[{.....}]
its working fine when I hit from browser but from my code data=[{}] are not sending.
My last attempts are :
1--->
String serviceUrl = "http://xxxxx/xxx/abc.php";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("id", "xx");
httpPost.setHeader("name", "xxx");
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(jsonArr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
2--->
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "xx") );
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
nameValuePairs.add(new BasicNameValuePair("data", jsonArr.toString()));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
StringEntity entity = new StringEntity(nameValuePairs.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
3--->
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id", "xx") );
nameValuePairs.add(new BasicNameValuePair("name", "xxx"));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(serviceUrl);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
StringEntity entity = new StringEntity(jsonArr.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
json_str = EntityUtils.toString(httpEntity);
Output :
Getting response same as that i got with url 'http://xxxxx/xxx/abc.php?id=xx&name=xxx' (without data parameter) on browser,
i thing it means data=[{}] are not sending with simple_string_parameters from my code.
Tried lot of way to send data using HttpPost but not succeed
=> Yes its obvious as your webservice URL is following a GET method, not a POST.
http://xxxxx/xxx/abc.php?id=xx&name=xxx&data=[{.....}]
So I would suggest you to try HTTPGet method instead of HTTPPost.
Check this answer for adding parameters to a HTTP GET request in Android?

Android POST JSON OBJECT - How to set $_POST Index

i have a JSONObject which i want to POST to a server.
Here is the Code:
JSONObject obj = new JSONObject();
for(int k = 0; k<len;k++){
obj.put("nachrichten_ids", params[k]);
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("xxxxx");
HttpEntity entity;
StringEntity s = new StringEntity(obj.toString());
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
entity = s;
httpPost.setEntity(entity);
HttpResponse response;
response = httpClient.execute(httpPost);
By doing Log.i("TEST",obj) i get the JSON object:
{"nachrichten_ids":"[2144,2138]"}
That data is send to the server. But i cant access it:
There is no $_POST index. (PHP)
How to set a index, so that i can access the json object, like $_POST['nachrichten_ids'].
I had to work with that data then e.g with php json_decode()
Any idea ?
Thanks
Try putting your jSON object into a namevaluepair. The name of the pair you add is the name you should use when reading it on the web. For example here I will get Password by calling $_POST['Password'].
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Password", "My password"));
nameValuePairs.add(new BasicNameValuePair("Mail", "My mail"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
BasicHttpResponse response = (BasicHttpResponse) httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

how to pass arralylist in basic namevalue pairs in android

Hello Sir i want send list of data to php server i use following code is it correct or not please tell me
i try this code
ArrayList<String>list1=new ArrayList<String>();
for(int i=0;i<json.length();i++)
{
JSONObject e = json.getJSONObject(i);
list1.add(e.getString("menuname"));
}
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("menuname",list1.toString()));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.103/update.php");
// "http://192.168.1.12/addnotes.php");
//url+"Notes/addnotes.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Toast.makeText(getApplicationContext(),
"Saved Sucessfully", Toast.LENGTH_SHORT).show()
please see the above code is correct or not .. please tell me
i think you want to get content in String right than use this
String mResponseText = EntityUtils.toString(entity);
insetad of
is= entity.getContent();
may it helps you

Categories

Resources