gpt4 book ai didi

java - 如何从 HttpClient 切换到 HttpUrlConnection?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:30 25 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序,我通过 HttpClient 将数据从 Android 应用程序发送到 servlet。我使用 HttpPost 方法。

我在 Android 开发者网站上读到 Apache HttpClient 库在 Android Froyo 2.2 中有一些错误,毕竟使用 HttpUrlConnection 而不是 HttpPost 是个好习惯。所以我想将我的 HttpPost 代码转换为 HttpUrlConnectio 但不知道如何。

我在这里发布我的 Android 代码和 servlet 代码

安卓代码

private String postData(String valueIWantToSend[]) 
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1",valueIWantToSend[0]));
nameValuePairs.add(new BasicNameValuePair("param2", valueIWantToSend[1]));
nameValuePairs.add(new BasicNameValuePair("param3", valueIWantToSend[2]));
nameValuePairs.add(new BasicNameValuePair("param4", valueIWantToSend[3]));
nameValuePairs.add(new BasicNameValuePair("param5", valueIWantToSend[4]));
nameValuePairs.add(new BasicNameValuePair("param6", valueIWantToSend[5]));
nameValuePairs.add(new BasicNameValuePair("param7", valueIWantToSend[6]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
/* execute */
HttpResponse response = httpclient.execute(httppost);
HttpEntity rp = response.getEntity();

//origresponseText=readContent(response);
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
}
catch (IOException e)
{
// TODO Auto-generated catch block
}
return null;
}

这是我的servlet代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
Enumeration paramNames = request.getParameterNames();
String params[] = new String[7];
int i=0;

while(paramNames.hasMoreElements())
{
String paramName = (String) paramNames.nextElement();
System.out.println(paramName);


String[] paramValues = request.getParameterValues(paramName);
params[i] = paramValues[0];

System.out.println(params[i]);

i++;
}

}

最佳答案

当我阅读 already mentioned Google post关于在较新版本的 Android 中执行 HTTP 请求的最佳实践,我以为有人在开玩笑。 HttpURLConnection 与几乎所有其他与 HTTP 服务器通信的方式(除了直接 Socket 通信)相比,使用起来真是一场噩梦。

我没有找到一个非常精简的 Android 库来完成繁重的工作,所以我自己写了一个。您可以在 DavidWebb 找到它包括 a list of alternative libraries这是我在开发库后(不幸地)发现的。

您的代码或多或少看起来像这样:

public void testPostToUrl() throws Exception {
String[] values = new String[3];

Webb webb = Webb.create();
Response<String> response = webb
.post("http://www.example.com/abc.php")
.param("param1", values[0])
.param("param2", values[1])
.param("param3", values[2])
.asString();

assertEquals(200, response.getStatusCode());
assertNotNull(response.getBody());
assertTrue(response.getBody().contains("my expected result"));
}

public void testPostToUrlShorter() throws Exception {
String[] values = new String[3];

Webb webb = Webb.create();
String result = webb
.post("http://www.example.com/abc.php")
.param("param1", values[0])
.param("param2", values[1])
.param("param3", values[2])
.ensureSuccess()
.asString()
.getBody();

assertTrue(result.contains("my expected result"));
}

关于java - 如何从 HttpClient 切换到 HttpUrlConnection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26898667/

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