2

我正在尝试使用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();
            }                       
}
4

3 回答 3

2

而不是使用BASE64.DEFAULT,尝试使用BASE64.NO_WRAP。这将帮助您解决问题。

于 2013-08-12T07:31:58.293 回答
1

改变这个:

String base64EncodedCredentials =Base64.encodeToString(data, Base64.DEFAULT);

对此:

String base64EncodedCredentials =Base64.encodeToString(data, Base64.NO_WRAP|Base64.URL_SAFE);

希望这可以帮助。

于 2013-08-12T07:34:46.717 回答
0

你为什么不使用这个:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("key_of_data", data));            

HttpPost oHttpPost = new HttpPost(your_url);
oHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

而不是使用Base64编码

于 2013-08-12T07:40:07.560 回答