我试图使用这样的POST命令:
http://123.456.78.9/Dev/SignIn?Username=email%40google.edu&Password=thePassword
在我的Android应用中,我已经查看了像HTTPConnection这样的内容,但我仍然不确定拨打电话的最佳方式是什么,我需要包含多个密钥和值
我现在正在尝试这个:
URl url = new URL("http://123.456.78.9/Dev/SignIn?");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", emailText));
params.add(new BasicNameValuePair("Password", passwordText));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
这会完成这项工作吗?如果我要使用这种方法,如何获得响应呢?
答案 0 :(得分:-1)
您可以使用Android Asynchronous Http Client library:
final AsyncHttpClient asyncHttpClient=new AsyncHttpClient();
//Add Parameter
RequestParams requestParams=new RequestParams();
requestParams.put("email", emailText);
requestParams.put("Password", passwordText);
//Chose one from two method in below
//send with get method
asyncHttpClient.get("http://123.456.78.9/Dev/SignIn", requestParams, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
System.out.println(responseString);
}
});
//send with post method
asyncHttpClient.post("http://123.456.78.9/Dev/SignIn", requestParams, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
System.out.println(responseString);
}
});
但你必须在你的gradle上添加这个库
compile 'com.loopj.android:android-async-http:1.4.9'