我正在尝试使用HTTPPOST
. 我已经使用BASE64.encodetoString()
. 但我无法发布数据。没有错误,但我每次都会收到400 或 404 响应代码。代码在 Java 中成功执行,但在 android 中没有按预期工作。请帮我。
这是我正在使用的代码:
public void getLoginInfo()
{
String user="xxxx@gmail.com";
String password="1234xyz";
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(url);
// String base64EncodedCredentials = "Basic " + Base64.encodeToString((user + ":" + password).getBytes(),0);
// Building post parameters
// key and value pair
byte[] data=(user+":"+password).getBytes();
String base64EncodedCredentials =Base64.encodeToString(data, Base64.DEFAULT);
String httpheader="Basic "+base64EncodedCredentials;
System.out.println("httpheader "+httpheader);
/* List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("email", "nishanth.s@giwitservices.com"));
nameValuePair.add(new BasicNameValuePair("password","100006438166763hbsV1v0"));*/
// Url Encoding the POST parameters
// Making HTTP Request
// try {
try {
httpPost.setHeader("Authorization", httpheader);
//httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpClient.execute(httpPost);
System.out.println("response test "+response.toString());
System.out.println("response code "+response.getStatusLine().getStatusCode());
String the_string_response = convertResponseToString(response);
System.out.println("respones "+the_string_response);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}