gpt4 book ai didi

android - 使用 HttpUrlConnection 向服务器发送请求

转载 作者:搜寻专家 更新时间:2023-11-01 09:46:07 24 4
gpt4 key购买 nike

我介于两者之间。我想使用 HttpUrlConnection 实现一个 POST 方法来发布用于将用户注册到服务器的电子邮件、名称和密码。这是我的代码:

public void createNewProfile(View view){

new Post().execute("http://myurl.com");

}

private class Post extends AsyncTask<String, Void, String>{

@Override
protected String doInBackground(String... params) {

try {

URL url = new URL("http://myurl.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);

ContentValues values = new ContentValues();
values.put("email", "abc@xyz.com");
values.put("password", 123);
values.put("name","ABC");

OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(values));
writer.flush();
writer.close();
os.close();
response = conn.getResponseCode();
conn.connect();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Result",String.valueOf(e));
}
return null;
}

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() +" "+ String.valueOf(response));

return result.toString();
}
}

我不知道我哪里弄错了。我收到以下回复

name=ABC&email=abc%40xyz.com&password=123 0

其中空格后的“0”为http响应码返回的响应码。当我在浏览器中尝试时,我的 URL 是正确的。我不知道我在哪里犯了错误;是我的服务器故障还是我的代码有错误,因为我认为我的代码没有进行任何交互处理。

我是 Android 开发的初学者,尝试了很多次和不同的代码,但出现错误。

请帮忙!提前致谢。

最佳答案

尝试这样的事情:

try {
url = new URL(params[0]);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");



outputStream = httpURLConnection.getOutputStream();

bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(myValues);
bufferedWriter.flush();

int statusCode = httpURLConnection.getResponseCode();
Log.d(Constants.LOG_TAG, " The status code is " + statusCode);

if (statusCode == 200) {
inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
response = convertInputStreamToString(inputStream);
Log.d(Constants.LOG_TAG, "The response is " + response);

JSONObject jsonObject = new JSONObject(response);

return true;

} else {
return false;
}

} catch (Exception e) {

e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

关于android - 使用 HttpUrlConnection 向服务器发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37997163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com