android 发送http请求方法和异常解决方法

String httpUrl = "http://api.hcl100.com/index.php/Applist/search/paging/0";
                    /* 建立HTTP Get对象 */
                    HttpGet httpRequest = new HttpGet(httpUrl);
                    try {
                        /* 发送请求并等待响应 */
                        HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
                        /* 若状态码为200 ok */
                        if (httpResponse.getStatusLine().getStatusCode() == 200) {
                            /* 读 */
                            String strResult = EntityUtils.toString(httpResponse
                                    .getEntity());
                            /* 去没有用的字符 */
                            System.out.println(strResult);
                        } else {
                            System.out.println("123123");
                        }
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


出现new DefaultHttpClient().execute(httpRequest)空的话 另开线程解决

你可能感兴趣的:(android)