[英]How to do POST request and get a response from server?
I need to do a POST request to SERVER by such example: 我需要通过以下示例向SERVER发送POST请求:
REQUEST FORMAT: 请求格式:
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
Now, I have been done a transfer for headers: 现在,我已经完成了头文件的传输:
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;
}
}
//.............................................
}
How can I do a transfer for parameters(client_id, response_type, redirect_uri, scope)? 如何传输参数(client_id,response_type,redirect_uri,范围)? And how can I get response from server?
以及如何从服务器获得响应? RESPONSE EXAMPLE:
响应示例:
HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=i1WsRn1uB1ehfbb37
In your doInBackground you can try something like this. 在您的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;
and In onPostExecute you can do whatever with the reponse. 在onPostExecute中,您可以对响应执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.