通过HttpUrlConnection发布在服务器上注册用户数据的请求 [英] Post request for registering user data on server by HttpUrlConnection
问题描述
我目前正在开展一个项目,我需要在该项目中发送用户的用户名,密码和电子邮件,以便在我的服务器上注册用户。我使用POST命令,因为我必须发送3个值来注册用户,我使用ContentValues来达到这个目的。这是我的代码:
I am currently working on a project in which I need to send username,password and email of a user for registering the user on my server. I used POST command for that and as I have to send 3 values for register a user,I used ContentValues for that purpose. Here is my code:
@Override
protected String doInBackground(String... params) {
try {
url = new URL(params[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
ContentValues values = new ContentValues();
values.put("email", "abc@xyz.com");
values.put("password", "123");
values.put("name","ABC");
outputStream = httpURLConnection.getOutputStream();
bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(getQuery(values));
bufferedWriter.flush();
statusCode = httpURLConnection.getResponseCode();
Log.i("Result",String.valueOf(statusCode));
inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
int data = inputStreamReader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = inputStreamReader.read();
}
JSONObject jsonObject = new JSONObject(result);
Log.i("Result",String.valueOf(jsonObject));
if (statusCode == 200) {
inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
Log.i("Result","Correct Data Returned");
JSONObject jsonObject = new JSONObject(response);
return true;
} else {
Log.i("Result","Data not returned");
return false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
这是我的getQuery方法:
Here is my getQuery method:
private String getQuery(ContentValues values) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, Object> entry : values.valueSet())
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8"));
}
Log.i("Result",result.toString());
return result.toString();
}
但我收到了以下回复:
Result: name=ABC&email=abc%40xyz.com&password=123
Result: 422
System.err: java.io.FileNotFoundException: http://docpanel.myworkdetails.com/api/auth/register
422是responseCode返回的状态代码,表示:
Where 422 is the status code returned by responseCode that means :
"errors": {
"email": [
"The email field is required."
],
"password": [
"The password field is required."
],
"name": [
"The password field is required."
]
},
"status_code": 422
I我没有得到如何通过POST方法传递争论,以使我的注册页面工作。我有正确的URL。
I am not getting how to pass arguements by POST method so as to make my signup page working. And I have the correct URL.
这是服务器端故障还是我在实施POST时出错?
Is this server side fault or I am making mistakes in implementing POST?
请帮忙!
提前致谢。
Please Help! Thanks in advance.
推荐答案
解决
我犯了错误,我没有在正确的地方添加 httpUrlConnection.connect(),还有 httpURLConnection.setRequestProperty(接受, / )在正确的地方使它工作正常。所以这里是使用HttpUrlConnection的POST请求用于使signUp页面成为可能的正确代码:
I was making mistakes that I didn't add httpUrlConnection.connect() at right place and also httpURLConnection.setRequestProperty("Accept","/") at right place made it working fine. So here is the correct code by which POST request using HttpUrlConnection is used to make signUp page possible:
protected String doInBackground(String... params) {
try {
url = new URL(params[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept","*/*");
ContentValues values = new ContentValues();
values.put("email", "abc@tjk.com");
values.put("password", "hjh");
values.put("name","hui");
httpURLConnection.connect();
outputStream = httpURLConnection.getOutputStream();
bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(getQuery(values));
bufferedWriter.flush();
statusCode = httpURLConnection.getResponseCode();
inputStream = httpURLConnection.getInputStream();
if (statusCode == 200) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
int data = inputStreamReader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = inputStreamReader.read();
}
JSONObject jsonObject = new JSONObject(result);
Log.i("Result",String.valueOf(jsonObject));
} else {
Log.i("Result","false");
return false;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
这篇关于通过HttpUrlConnection发布在服务器上注册用户数据的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!