与HttpURLConnection类类多重困境在Java中为Android [英] Multiple Difficulties with HttpURLConnection class in Java for Android

查看:166
本文介绍了与HttpURLConnection类类多重困境在Java中为Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- Update--结果
对于那些谁帮我道歉,原来这仅仅是一个Eclipse的调试问题。怀疑这是导致我错了以后,我把下来一对夫妇的System.out.println的要观察的变量,并根据他们,他们正在改变,调试器只是带我旧的信息无论出于何种原因。不知道为什么会发生的事情,但重要的是,code显然不实际工作。

--Update--
Apologies for those who helped me, it turns out this is just a problem with Eclipse's debugger. After suspecting that it was leading me wrong, I placed down a couple of System.out.println to watch the variables, and according to them they ARE being changed, and that the debugger was just showing me old information for whatever reason. No clue why that's happening, but the important thing is that the code does apparently actually work.

我的工作方法与Twitter为Android应用程序共享,并建立HttpURLConnection的时候我有错误。我创建了连接对象按照往常一样,使用url的OpenConnection函数然后将其转换为一个HttpURLConnection类,当我随后连接上运行SetRequestMethod(POST),但它绝对没有。当我通过行运行code在调试器行,因为我通过该行的请求方法只是保持为默认值(GET)。任何人有任何想法,为什么这可能会发生?我越来越有setDoOutput(真正的)也没有任何改变同样的问题。然而,增加的请求属性并仍然有效。我一直在寻找周围,一直没能找到这个问题的任何东西,甚至没有其他人报告了这些问题。

I'm working on a method to share with twitter for an Android application, and I'm having errors when setting up the HttpURLConnection. I create the connection object as per usual, using the openconnection function of a url then casting it to a HttpURLConnection, and when I subsequently run SetRequestMethod("POST") on the connection, it does absolutely nothing. When I run the code in the debugger line by line, as I go through that line the request method just remains as the default ("GET"). Anyone have any idea as to why this may be happening? I'm getting the same problem with setDoOutput(true) also not changing anything. However, adding a request property does still work. I've been searching around and haven't been able to find anything on this problem, not even another person reporting these problems.

推荐答案

我不使用河畔是否HttpURLConnection类是这里最好的。

I am not sur whether using HttpURLConnection is the best here.

你试试下面的方法是什么?

Did you try the following way?

// Building the POST request
final BasicNameValuePair message   = new BasicNameValuePair("yourField", "yourContent");
final List<NameValuePair> list     = new ArrayList<NameValuePair>(1);

list.add(message);

final HttpPost httppost = createHttpPost(UrlEncodedFormEntity(list));

// Building the HTTP client
final HttpParams httpParameters = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(httpParameters, YOUR_CHOSEN_CONN_TIMEOUT);
HttpConnectionParams.setSoTimeout        (httpParameters, YOUR_CHOSEN_SO_TIMEOUT);

final HttpClient httpClient = new DefaultHttpClient(httpParameters);

// Execution of the POST request
final HttpResponse response = httpClient.execute(httppost);

这是我平时做的方式,没有任何问题。

This is the way I usually do, with no problems.

Apache的HttpClient的是升级Froyo和以前版本的最好的办法。现在,根据从Android开发者博客这篇文章( A),它是更好地使用URLConnection的

Apache's HttpClient was the best approach for Froyo and former versions. Now, according to this article from Android Developers Blog (written after this Q&A), it is better to use URLConnection.

这篇关于与HttpURLConnection类类多重困境在Java中为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆