Android REST HTTP在执行时接收{" status":" 400","错误":"错误请求"}

时间:2016-03-10 14:45:36

标签: android http android-asynctask httprequest httpclient

这是asynctask上的以下android代码,用于从网站获取数据。我还提供用户名,密码和参数。结果是{"状态":" 400","错误":"错误请求"}

public class 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;
}

1 个答案:

答案 0 :(得分:0)

我能够使用替代方法

接收字符串
System.Runtime.InteropServices

private const int SWP_NOSIZE = 0x1;
private const int WM_CTLCOLORLISTBOX = 0x0134;

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int
X, int Y, int cx, int cy, uint uFlags);

protected override void WndProc(ref Message m)
{
     if (m.Msg == WM_CTLCOLORLISTBOX)
     {
         // Make sure we are inbounds of the screen
         int left = this.PointToScreen(new Point(0, 0)).X;

         //Only do this if the dropdown is going off right edge of screen
         if (this.DropDownWidth > Screen.PrimaryScreen.WorkingArea.Width - left)
         {
            // Get the current combo position and size
            Rectangle comboRect = this.RectangleToScreen(this.ClientRectangle);

            int dropHeight = 0;
            int topOfDropDown = 0;
            int leftOfDropDown = 0;

            //Calculate dropped list height
            for (int i = 0; (i < this.Items.Count && i < this.MaxDropDownItems); i++)
            {
                dropHeight += this.ItemHeight;
            }

            //Set top position of the dropped list if 
            //it goes off the bottom of the screen
            if (dropHeight > Screen.PrimaryScreen.WorkingArea.Height -
                   this.PointToScreen(new Point(0, 0)).Y)
            {
                topOfDropDown = comboRect.Top - dropHeight - 2;
            }
            else
            {
                topOfDropDown = comboRect.Bottom;
            }

            //Calculate shifted left position
            leftOfDropDown = comboRect.Left - (this.DropDownWidth -
                   (Screen.PrimaryScreen.WorkingArea.Width - left));
            //when using the SWP_NOSIZE flag, cx and cy params are ignored
            SetWindowPos(m.LParam,
                         IntPtr.Zero,
                         leftOfDropDown,
                         topOfDropDown,
                         0,
                         0,
                         SWP_NOSIZE);
          }
      }

      base.WndProc(ref m);
}