Verwenden von cURL-Befehlen in Android
Ich habe den folgenden cURL-Befehl und möchte dasselbe von Android ausführen,
curl -X POST -H "Content-Type: application/json" -d
'{"username":"user","password":"pass"}'
http://www.somesite.com/login
Dies ist, was ich für Andorid gemacht habe,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.somesite.com/login");
try {
httppost.addHeader("Content-Type", "application/json");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "user"));
nameValuePairs.add(new BasicNameValuePair("password", "pass"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
Log.d("RESPOND", EntityUtils.toString(response.getEntity()));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Beim Ausführen der Anwendung wird anstelle des gewünschten JSON-Arrays die gesamte Seite als Antwort angezeigt. Ich bin neu in CURL und ich habe keine Ahnung, was mit dem Code los ist.