Android发送Http Get/Post请求并返回

public String getResultForHttpGet(String queryString){
        String fullQueryString = mDomainString + queryString;
        String result="";
        
        //取得HTTP response
        HttpResponse response;
        try {
            HttpClient httpClient= new DefaultHttpClient();
            HttpGet httpGet=new HttpGet(fullQueryString);
            response = httpClient.execute(httpGet);
            //若状态码为200
            if(response.getStatusLine().getStatusCode()==200){
                    //取出应答字符串
                    HttpEntity entity=response.getEntity();
                    result=EntityUtils.toString(entity, HTTP.UTF_8);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return result;
    }

注:mDomainString是域名,如http://localhost:8000

注意Mainfest需要添加权限

<uses-permission android:name="android.permission.INTERNET"/>

你可能感兴趣的:(Android发送Http Get/Post请求并返回)