如何做POST请求并从服务器获得响应?

时间:2014-08-06 19:55:08

标签: java android post

我需要通过这样的示例对SERVER发出POST请求:

请求格式:

POST /oauth/authorize HTTP/1.1
Host: m.sp-money.yandex.ru (для мобильных устройств) или sp-money.yandex.ru (для остальных  устройств)
Content-Type: application/x-www-form-urlencoded
Content-Length: <content-length>

client_id=<client_id>&response_type=code
&redirect_uri=<redirect_uri>&scope=<scope>

REQUEST PARAMETERS EXAMPLE:

client_id=092763469236489593523464667
response_type=code
redirect_uri=https://client.example.com/cb
scope=account-info operation-history

现在,我已完成标题转移:

protected Void doInBackground(Void... arg)
        {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("HOST", "sp-money.yandex.ru"));
            params.add(new BasicNameValuePair("Content-Type", "application/x-www-form-urlencoded"));
            params.add(new BasicNameValuePair("Content-Length", "154"));

            JSONObject testJSON = makeHttpRequest("https://money.yandex.ru/oauth/authorize", "POST", params);
            int test = 1;
            return null;
        }

public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) 
        {
            try 
            {
                if(method == "POST")
                {
                    String responseText = null;
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                    String testStr = httpPost.toString();
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    responseText = EntityUtils.toString(httpResponse.getEntity());                
                    int test = 1;
                    test = 0;
                }          
            }
//.............................................
}

如何进行参数传输(client_id,response_type,redirect_uri,scope)? 我如何从服务器获得响应? 回应示例:

HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=i1WsRn1uB1ehfbb37

1 个答案:

答案 0 :(得分:0)

在你的doInBackground中,你可以尝试这样的事情。

HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(HTTP_REQUEST_URL);

            //create and assign post request server data
            String latitude = loc.getLatitude() + "";
            String longitude = loc.getLongitude() + "";
            String time = DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()) + "";
            String whr = WhereAmI(loc.getLatitude(), loc.getLongitude());

            //data back from server
            String responseBackFromServer = "";

            try {
                List<NameValuePair> pairs = new ArrayList<NameValuePair>();

                pairs.add(new BasicNameValuePair("latitude", latitude));
                pairs.add(new BasicNameValuePair("longitude", longitude));
                pairs.add(new BasicNameValuePair("whereAmI", whr));
                pairs.add(new BasicNameValuePair("time", time));

                post.setEntity(new UrlEncodedFormEntity(pairs));
                HttpResponse server_response = client.execute(post);

                responseBackFromServer = EntityUtils.toString(server_response.getEntity());

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

            return "Response Back: " + responseBackFromServer;

并且在onPostExecute中你可以做任何响应。