l'authentification de base avec http post params android
Je suis en train de faire l'authentification de base en passant par nom d'utilisateur et le mot de passe et ensuite à l'aide de BasicNameValuePair
pour l'envoi de post params pour obtenir la réponse du service.
Ma méthode:
public StringBuilder callServiceHttpPost(String userName, String password, String type)
{
//Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(WEBSERVICE + type);
HttpResponse response = null;
StringBuilder total = new StringBuilder();
try {
URL url = new URL(WEBSERVICE + type);
/*String base64EncodedCredentials = Base64.encodeToString((userName
+ ":" + password).getBytes(), Base64.URL_SAFE
| Base64.NO_WRAP);*/
String base64EncodedCredentials = "Basic " + Base64.encodeToString(
(userName + ":" + password).getBytes(),
Base64.NO_WRAP);
httppost.setHeader("Authorization", base64EncodedCredentials);
//Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("day", Integer.toString(2)));
nameValuePairs.add(new BasicNameValuePair("emailId", "[email protected]"));
nameValuePairs.add(new BasicNameValuePair("month", Integer.toString(5)));
nameValuePairs.add(new BasicNameValuePair("year", Integer.toString(2013)));
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
}
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
InputStream instream = entity.getContent();
String line = "";
//Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(instream));
//Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return total;
}
Mais je suis arriver à ce total:
<html><head> <title>Status page</title></head><body><h3>Invalid json representation of content</h3><p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1">here</a>.<br>Please continue your visit at our <a href="/">home page</a>.</p></body></html>
Vous devez vous connecter pour publier un commentaire.
C'est la façon de le faire:
Il semble que le contenu que vous ajoutez est pas dans
JSON
format.Voici ce que je propose:
Préparer votre contenu dans
Map<String, String>
De le transformer en
JSON
, par exemple avecGson
(ajoutergson
à Maven ou de le mettre à votre dossier assets):et de transformer préparé carte avec
gson.toJson(inputMap)
.Résultant chaîne de caractères contenant le contenu dans
JSON
format ajouter à votrehttppost
comme ceci:Gson
vous aurez besoin de télécharger et de mettre àassets
dossier ou l'ajouter à Maven depom.xml
google-gson-2.2.4-release
et le mettre dans le dossier des actifs, mais son direGson cannot be resolved to a type
lorsque j'écris du codeprivate static final Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
gson-2.2.4.jar
.jsonContent
ne peut pas être résolu.String jsonContent = gson.toJson(inputMap);
Remplir le inputMap avec tous les paramètres nécessaires comme par votre exemple.411 Length Required
commehttp://stackoverflow.com/questions/15619562/getting-411-length-required-after-a-put-request-from-http-client
Cela fonctionne :