Android 网络

最基本的HTTP请求方法是GET。在这种请求中,发送所有数据使用查询字符串嵌在URLk。

Socket:

writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

HTTP:

 url = new URL(location);

HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

in.close();
urlConn.disconnect();

Android以Apache httpClient库的形式提供了另一组API,这些API进一步的抽象了java.net类,提供了更健壮的HTTP支持并可帮助你处理单独线程问题。


HTTPCLIENT

private void performRequest() {

       // use a response handler so we aren't blocking on the HTTP request
       final ResponseHandler responseHandler = new ResponseHandler () {

           public String handleResponse(HttpResponse response) {
               // when the response happens close the notification and update UI
               StatusLine status = response.getStatusLine();
               Log.d(Constants.LOGTAG, " " + ApacheHTTPSimple.CLASSTAG + " statusCode - " + status.getStatusCode());
               Log.d(Constants.LOGTAG, " " + ApacheHTTPSimple.CLASSTAG + " statusReasonPhrase - "
                   + status.getReasonPhrase());
               HttpEntity entity = response.getEntity();
               String result = null;
               try {
                   result = StringUtils.inputStreamToString(entity.getContent());
                   Message message = handler.obtainMessage();
                   Bundle bundle = new Bundle();
                   bundle.putString("RESPONSE", result);
                   message.setData(bundle);
                   handler.sendMessage(message);
               } catch (IOException e) {
                   Log.e(Constants.LOGTAG, " " + ApacheHTTPSimple.CLASSTAG, e);
               }
               return result;
           }
       };

       this.progressDialog = ProgressDialog.show(this, "working . . .", "performing HTTP request");

       // do the HTTP dance in a separate thread (the responseHandler will fire when complete)
       new Thread() {

           @Override
           public void run() {
               try {
                   DefaultHttpClient client = new DefaultHttpClient();
                   HttpGet httpMethod = new HttpGet(urlChooser.getSelectedItem().toString());
                   client.execute(httpMethod, responseHandler);
               } catch (ClientProtocolException e) {
                   Log.e(Constants.LOGTAG, " " + ApacheHTTPSimple.CLASSTAG, e);
               } catch (IOException e) {
                   Log.e(Constants.LOGTAG, " " + ApacheHTTPSimple.CLASSTAG, e);
               }
           }
       }.start();
   }


REST采用的主要概念是以URI的形式指定资源,然后使用不同的协议方法执行不同的操作。

private String parseXMLResult(String xmlString) {
       StringBuilder result = new StringBuilder();

       try {
           SAXParserFactory spf = SAXParserFactory.newInstance();
           SAXParser sp = spf.newSAXParser();
           XMLReader xr = sp.getXMLReader();
           DeliciousHandler handler = new DeliciousHandler();
           xr.setContentHandler(handler);
           xr.parse(new InputSource(new StringReader(xmlString)));

           List posts = handler.getPosts();
           for (DeliciousPost p : posts) {
               Log.d(Constants.LOGTAG, " " + DeliciousRecentPosts.CLASSTAG + " DeliciousPost - " + p.getHref());
               result.append("/n" + p.getHref());
           }
       } catch (Exception e) {
           Log.e(Constants.LOGTAG, " " + DeliciousRecentPosts.CLASSTAG + " ERROR - " + e);
       }
       return result.toString();
   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值