Android 使用HttpUrlConnection将请求发布到服务器

Android 使用HttpUrlConnection将请求发布到服务器,android,post,http-post,httpurlconnection,http-response-codes,Android,Post,Http Post,Httpurlconnection,Http Response Codes,我被夹在中间。我想实现一个POST方法,使用HttpUrlConnection发布电子邮件、名称和密码,以便向服务器注册用户。 这是我的密码: public void createNewProfile(View view){ new Post().execute("http://myurl.com"); } private class Post extends AsyncTask<String, Void, String>{ @Override pro

我被夹在中间。我想实现一个POST方法,使用HttpUrlConnection发布电子邮件、名称和密码,以便向服务器注册用户。 这是我的密码:

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();
    }
}
其中,空格后的“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();
    }
}

在finally语句中获取响应后,尝试关闭编写器将其设置为response=conn.getResponseCode()和conn.connect(),但同样的响应,我也删除了该响应。我已添加代码以供参考。。。检查并让我知道它是否有帮助我现在从这个代码中得到响应代码405。当我为任何响应代码运行inputStream时,再次执行fileNotFoundException。我输入的是我的数据表还是服务器端故障?我应该试试截击吗?你可以试试截击。。但我认为你的url或服务器端代码有问题。你确定代码没有问题吗?我可以使用此POST请求将特定用户注册到API url吗?可以。。从过去6个月以来,我一直在使用上述代码,关于POST请求?只需查询一些我在代码中描述的值,就可以用来注册用户?
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();
    }
}