安卓与后台进行数据交互

方式一:

public String updateRequest(String operation,String phone) { 
        String strResult = null;
        HttpPost httpRequest =new HttpPost(CommonURL.UpdateURL);     
        List <NameValuePair> params=new ArrayList<NameValuePair>();     
        params.add(new BasicNameValuePair("Operation",operation));  
        params.add(new BasicNameValuePair("Phone",phone)); 
        try{  
            httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
            HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
            if(httpResponse.getStatusLine().getStatusCode()==200){     
                //取出回应字串
                strResult=EntityUtils.toString(httpResponse.getEntity()); 
                Pattern titleP = Pattern.compile("<body>(.*\r\n)*</body>");
                Matcher matchedTitle = titleP.matcher(strResult);
                Boolean foundTitle = matchedTitle.find();
                if(foundTitle){
                    strResult = matchedTitle.group(1);
                }

            }else{     
                Log.e("请求结果:", "请求失败!");  
            }     
        }catch(ClientProtocolException e){     

            e.printStackTrace();     
        } catch (UnsupportedEncodingException e) {     

            e.printStackTrace();     
        } catch (IOException e) {     
            e.printStackTrace();     
        } 

        return strResult;
    }

方式二:

public String sendRequest(JSONObject object,String MyURL) {
        String jsonStr=null;
        try {  
            URL url = new URL(MyURL);  
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            conn.setRequestMethod("POST");  
            conn.setRequestProperty("ser-Agent", "Fiddler");  
            conn.setRequestProperty("Content-Type", "application/json");  
            conn.setConnectTimeout(5 * 1000); 
            // 包装并上传数据  
            OutputStream outputStream = conn.getOutputStream();  
            outputStream.write(URLEncoder.encode(object.toString(), "UTF-8").getBytes());  

            // 如果请求响应码是200,则表示成功  
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {  

                //获取服务器上的数据  
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));  
                //解码  
                jsonStr = URLDecoder.decode(in.readLine(), "UTF-8");  
                in.close();  
                conn.disconnect();  
            }  
        } catch (MalformedURLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (ProtocolException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }catch (SocketTimeoutException e) { 
            jsonStr = "连接超时";

        }catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }

        return jsonStr;

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值