奔跑的西瓜小潘 2014-08-31 12:03 采纳率: 0%
浏览 2186

java新手,想问问有没有大牛给我解释一下这段代码,就是关于http报文传输的

需要添加httpmime包

这个是发post的method:
public static String sendPostRequest(String url, List nameValuePairs) {
// add code to validate required fields

    String res = null;

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        if (ListUtils.isNotEmpty(nameValuePairs)) {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }
        HttpResponse response = httpclient.execute(httppost);
        res = getResponseText(response);

        // add code check respond status

    } catch (ClientProtocolException e) {
        if (listener != null) {
            listener.onError(null);
        }
        CLog.e(HttpUtils.class, e.getMessage() + "");
    } catch (IOException e) {
        if (listener != null) {
            listener.onError(null);
        }
        CLog.e(HttpUtils.class, e.getMessage() + "");
    } catch (IllegalStateException e) {
        CLog.e(HttpUtils.class, e.getMessage());
        if (listener != null) {
            listener.onError(null);
        }
    }
    return res;
}

调用example:
List params = new LinkedList();

params.add(new BasicNameValuePair("pid", Integer.toString(pid)));

params.add(new BasicNameValuePair("comment", comment));

HttpUtils.sendPostRequest(“http://192.168.1.1/test.php”, params);

  • 写回答

2条回答

  • c920816698 2014-09-04 02:52
    关注

    你最好先去了解一下http的请求方式,和请求步骤,在对着代码,查查API就能看懂了

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试