【问题标题】:Use HttpURLConnection instead of HttpPost使用 HttpURLConnection 而不是 HttpPost
【发布时间】:2015-05-06 06:25:51
【问题描述】:

我有一个用 java 编写的用于 android 应用程序的代码。通过使用 HttpPost 和 DefaultHttpClient 库。目前,我正在重新编码它以用 HttpURLConnection 替换 HttpPost 和 DefaultHttpClient 库,因为 DefaultHttpClient 已被贬低。 我已经为我的一个项目完成了它,并且成功了。

但在当前项目中,使用 HttpURLConnection 而不是 DefaultHttpClient 时,我没有从 web 服务获得相同的响应。谁能帮我解决我做错的地方?

这是旧代码:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String postParameter = "Param1=" + Value1 + "&Param2="+ Value2+ "&Param3="+Value3;
try {
 httpPost.setEntity(new StringEntity(postParameter));
} catch (Exception e) {
 e.printStackTrace();
}
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);

这是我的新代码

_Url = new URL(Url);
HttpURLConnection urlconnection = (HttpURLConnection) _Url.openConnection();
urlconnection.setRequestMethod(Type);
urlconnection.setConnectTimeout(Timeout);
urlconnection.setUseCaches(false);
urlconnection.setDoInput(true);
urlconnection.setDoOutput(true);
 String postParameter = "Param1=" + Value1 + "&Param2="+ Value2+ "&Param3="+Value3;
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os));
writer.write(postParameter);
writer.flush();
writer.close();
os.close();
urlconnection.connect();

代码运行没有任何错误,但 web 服务给出的响应与旧代码不同。

【问题讨论】:

    标签: java android http-post httpurlconnection


    【解决方案1】:

    你没有得到输入流,试试下面的代码

             try {
    
                    String postParameter = "Param1=" + Value1 + "&Param2="+ Value2+ "&Param3="+Value3;
                    URL url = new URL(UrlStr);
                    HttpURLConnection urlConnection = (HttpURLConnection) url
                            .openConnection();
                    urlConnection.setRequestMethod("POST");
                    urlConnection.setRequestProperty("Accept",
                            "application/json");
                    urlConnection.setRequestProperty("Content-Type",
                            "application/json");// setting your headers its a json in my case set your appropriate header
    
                    urlConnection.setDoOutput(true);
                    urlConnection.connect();// setting your connection
    
                    OutputStream os = null;
                    os = new BufferedOutputStream(
                    urlConnection.getOutputStream());
                    os.write(postParameter.toString().getBytes());
                    os.flush();// writing your data which you post
    
    
                    StringBuffer buffer = new StringBuffer();
                    InputStream is = urlConnection.getInputStream();
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(is));
                    String line = null;
                    while ((line = br.readLine()) != null)
                        buffer.append(line + "\r\n");
                         // reading your response 
    
                    is.close();
                    urlConnection.disconnect();// close your connection
                    return buffer.toString();
    
                } catch (Exception e) {
                }
    

    【讨论】:

    • 对不起,我没有写我的代码来获取我的问题的输入流,而是在我的应用程序中写了它。我认为问题不在于输入流。它与实体和输出流有关。 httpPost 正在使用 setEntity 方法,并且我已将 outputstream 与 openconnection 一起使用。与写入输出流相比,我认为 setEntity 方法对参数做了一些不同的事情
    • 您的请求方法是什么? urlconnection.setRequestMethod(Type);
    • 试试上面的代码,看看你得到了什么响应代码?
    【解决方案2】:

    试试这个

    List nameValuePairs = new ArrayList(3);
                nameValuePairs.add(new BasicNameValuePair("Param1", value1));
                nameValuePairs.add(new BasicNameValuePair("Param2", value2));
                nameValuePairs.add(new BasicNameValuePair("Param3", value3));
    
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs);
    
    OutputStream post = request.getOutputStream();
                entity.writeTo(post);
                post.flush();
    

    【讨论】:

    • 试过了,但得到的响应与我的代码相同:(
    • 您收到服务器的任何响应了吗?状态码等??
    • 是的,你的代码得到的响应与我的新代码相同,服务器响应错误。但原来的代码是正确的
    • 与写入输出流相比,httpPost 的 setEntity 方法是否对参数做了一些不同的事情??
    猜你喜欢
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多