Android REST HTTP在执行时接收{“状态”:“400”,错误::“错误请求”}

Android REST HTTP在执行时接收{“状态”:“400”,错误::“错误请求”},android,http,android-asynctask,httprequest,httpclient,Android,Http,Android Asynctask,Httprequest,Httpclient,以下是asynctask上的android代码,用于从网站获取数据。我还提供了用户名、密码和参数。结果是{“status”:“400”,“error”:“Bad Request”} 公共类MyAsyncTask扩展了AsyncTask{ HttpURLConnection urlConnection; List<NameValuePair> params; @Override protected String doInBackground(String... args) {

以下是asynctask上的android代码,用于从网站获取数据。我还提供了用户名、密码和参数。结果是{“status”:“400”,“error”:“Bad Request”}

公共类MyAsyncTask扩展了AsyncTask{

HttpURLConnection urlConnection;
List<NameValuePair> params;
@Override
protected String doInBackground(String... args) {

    String username = "usay";
    String password = "isc00l";
     InputStream is = null;
    StringBuilder sb;
     JSONObject jObj = null;
     String json = "";
    String result="";
    String unp = username+":"+password;
    params = new ArrayList<NameValuePair>(1);
    params.add(new BasicNameValuePair("access_token","6eebeac3dd1dc9c97a06985b6480471211a777b39aa4d0e03747ce6acc4a3369"));
    try {
        sb = new StringBuilder();
        HttpClient httpclient = new DefaultHttpClient();
        // HttpPost httppost = new HttpPost(domain.trim()+"/qhms/wse_hmlogin.php?loginid="+user.trim()+"&logpass="+pass.trim());
        // HttpPost httppost = new HttpPost(domain+"/qhms/wse_hmlogin.php?loginid="+user+"&logpass="+pass);
        // HttpPost httppost = new HttpPost("http://svv.in.net/service/index.php?user=Customer&pass=cus&des=rtr%20r%20rtr%20trtrt");
        HttpPost httppost = new HttpPost("https://www-staging.usay.co/app/surveys.json");
        //if(params !=null){
            httppost.setEntity(new UrlEncodedFormEntity(params));
       // }
        // httppost.setHeader( "Authorization","Basic "+"admin:admin");
        String encoded_login = Base64.encodeToString(unp.getBytes(), Base64.NO_WRAP);
        httppost.setHeader(new BasicHeader("Authorization", "Basic "+encoded_login));

        System.out.println("URL="+httppost.toString());

        // HttpPost httppost = new HttpPost("http://quantumr.info/qrms/call_service/wse_getitem.php?compkey=astres1312");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection" + e.toString());
    }
    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");

        String line = "0";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }
    Log.e("result",result);
    return result;
}
HttpURLConnection-urlConnection;
列出参数;
@凌驾
受保护的字符串doInBackground(字符串…args){
字符串username=“usay”;
字符串密码=“isc00l”;
InputStream=null;
为某人做准备;
JSONObject jObj=null;
字符串json=“”;
字符串结果=”;
字符串unp=用户名+“:”+密码;
params=新的ArrayList(1);
参数添加(新的基本名称对(“访问令牌”,“6eebeac3dd1dc9c97a06985b6480471211a777b39aa4d0e03747ce6acc4a3369”);
试一试{
sb=新的StringBuilder();
HttpClient HttpClient=新的DefaultHttpClient();
//HttpPost-HttpPost=new-HttpPost(domain.trim()+”/qhms/wse_hmlogin.php?loginid=“+user.trim()+”&logpass=“+pass.trim());
//HttpPost-HttpPost=new-HttpPost(domain+“/qhms/wse_hmlogin.php?loginid=“+user+”&logpass=“+pass”);
//HttpPost HttpPost=新的HttpPost(“http://svv.in.net/service/index.php?user=Customer&pass=cus&des=rtr%20r%20rtr%20trtrt");
HttpPost HttpPost=新的HttpPost(“https://www-staging.usay.co/app/surveys.json");
//如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
// }
//setHeader(“授权”、“基本”+“管理员:管理员”);
String encoded_login=Base64.encodeToString(unp.getBytes(),Base64.NO_WRAP);
setHeader(新的BasicHeader(“授权”、“基本”+编码的_登录));
System.out.println(“URL=“+httppost.toString());
//HttpPost HttpPost=新的HttpPost(“http://quantumr.info/qrms/call_service/wse_getitem.php?compkey=astres1312");
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
Log.e(“结果”,结果);
返回结果;
}

我能够使用另一种方法接收字符串

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("https://www-staging.usay.co/app/surveys.json?access_token=6eebeac3dd1dc9c97a06985b6480471211a777b39aa4d0e03747ce6acc4a3369");
httpGet.addHeader(BasicScheme.authenticate(
                new UsernamePasswordCredentials("usay", "isc00l"),
                "UTF-8", false));
try
{
    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity responseEntity = httpResponse.getEntity();
    inputStream = responseEntity.getContent();

}
catch(Exception e)
{
    e.printStackTrace();
}
try
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
    stringBuilder = new StringBuilder();
    stringBuilder.append(reader.readLine() + "\n");

    String line = "0";
    while ((line = reader.readLine()) != null)
    {
        stringBuilder.append(line + "\n");
    }
    inputStream.close();
    result = stringBuilder.toString();
}
catch (Exception e)
{
    Log.e("log_tag", "Error converting result " + e.toString());
}
Log.e("result",result);
return result;

谁告诉你用这种方式做请求的?不确定这个请求有什么问题,我已经将它与其他url一起使用,它能够接收到值。