使用Post的Android登录表单

使用Post的Android登录表单,android,post,http-post,Android,Post,Http Post,我正在尝试创建对以下api的post请求: 为此,我编写了以下代码: HttpPost httpPost = new HttpPost("http://consumer.api.mobdev.machies.com/v3/logins"); try { // Add user name and password EditText uname = (EditText)findViewById(R.id.username); String us

我正在尝试创建对以下api的post请求:

为此,我编写了以下代码:

    HttpPost httpPost = new HttpPost("http://consumer.api.mobdev.machies.com/v3/logins");

    try {
        // Add user name and password
     EditText uname = (EditText)findViewById(R.id.username);
     String username = uname.getText().toString();

     EditText pword = (EditText)findViewById(R.id.password);
     String password = pword.getText().toString();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("grant_type","password"));
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        httpPost.setHeader("Host","http://consumer.api.mobdev.machies.com");
        httpPost.setHeader("Content-Type","application/xml");
        httpPost.setHeader("Accept-Language","en-US");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        Log.w("Post", "Execute HTTP Post Request");
        HttpResponse response = httpclient.execute(httpPost);

        String str = inputStreamToString(response.getEntity().getContent()).toString();
HttpPost-HttpPost=新的HttpPost(“http://consumer.api.mobdev.machies.com/v3/logins");
试一试{
//添加用户名和密码
EditText uname=(EditText)findViewById(R.id.username);
字符串username=uname.getText().toString();
EditText pword=(EditText)findViewById(R.id.password);
字符串密码=pword.getText().toString();
List nameValuePairs=新的ArrayList(3);
添加(新的BasicNameValuePair(“授权类型”、“密码”);
添加(新的BasicNameValuePair(“用户名”,username));
添加(新的BasicNameValuePair(“密码”,password));
setHeader(“主机”http://consumer.api.mobdev.machies.com");
setHeader(“内容类型”、“应用程序/xml”);
setHeader(“接受语言”、“en-US”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
w(“Post”,“执行HTTP Post请求”);
HttpResponse response=httpclient.execute(httpPost);
字符串str=inputStreamToString(response.getEntity().getContent()).toString();

str指的是我得到的响应。在logcats中打印它时,我得到了以下一个响应,即无效主机名。我在代码中输入的主机名是否有问题?请帮助,因为我不熟悉使用rest样式的API

可能在这里使用HttpClient,它可能会对您有所帮助

          HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(strUrl);
    if (mHeader != null) {
        for (Header header : mHeader) {
            httpPost.addHeader(header);
        }
    }
    try {
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        if (nameValuePair != null) {
            ContentBody cb;
            for(NameValuePair value: nameValuePair){
                cb =  new StringBody(value.getValue(),"", null);
                entity.addPart(value.getName(), cb);
            }
        }
        if(filepath != null && filepath.length() > 0){
            File f = new File(filepath);
            entity.addPart("uploadedfile", new FileBody(f));
        }
        httpPost.setEntity(entity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        response = EntityUtils.toString(httpResponse.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        //Display();


    } catch (IOException e) {
        e.printStackTrace();
        //Display();            
    }

    if (mIsProgress)
        progress.dismiss();
    return response;
}

这意味着你的应用程序url有些错误。请检查你在http post方法中传递的url。你需要在单独的线程上执行http请求。请阅读以下内容:应用程序没有崩溃。如果我从服务器收到的响应说无效的主机名。至于将代码放入工作线程。我知道这一点,我只是以这种方式对s执行了此操作ee,如果它真的有效,并且在这种情况下会添加线程部分。如果您查看您提供的链接,“针对早期SDK版本的应用程序可以在其主事件循环线程上进行联网,但这是非常不鼓励的。”因此它应该可以工作,但速度很慢。因此它不是“主机”标题?我试图在浏览器中点击它。它说找不到服务器