android通过http访问mysql或者sqlserver oracle数据库

fibre 贡献于2013-09-26

作者 微软用户  创建于2012-05-23 08:29:00   修改者微软用户  修改于2012-05-23 08:31:00字数2896

文档摘要:android通过http访问mysql或者sqlserver oracle数据库
关键词:

看到网上很多童鞋都在问这个东东,我就发上来算了,哥都搞EE了,算了,为了方便大家还是给发布一下: package com.shuangchuan; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; public class MyData { public void post(String []str){ /**   * 使用httpclent包   * 直接通过HTTP协议提交数据到服务器,实现如下面表单提交功能:   * @paramactionUrl 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,由于它会指向手机模拟器,   * 你可以使用 itcast.cn 或 192.168.1.10:8080这样的路径测试)   *@paramparams 请求参数 key为参数名,value为参数值   *@paramfile 上传文件   **/ try { String s="http://192.168.1.105:8080/yingyong/servlet/accessData"; s=s+"?name=wei"; URL url=new URL(s); HttpURLConnection con=(HttpURLConnection )url.openConnection(); con.setConnectTimeout(5*1000);//设置超时时间 con.setDoInput(true);//答应输进 con.setDoOutput(true);//答应输出 con.setUseCaches(false);//不使用Cache con.setRequestMethod("POST"); con.setRequestProperty("Charaset", "UTF-8"); StringBuilder sb=new StringBuilder(); sb.append("--start".getBytes()); sb.append(str[0]); sb.append(str[1]); sb.append(str[2]); DataOutputStream out=new DataOutputStream(con.getOutputStream()); out.write(sb.toString().getBytes()); out.write("--end".getBytes()); int cah=con.getResponseCode(); if(cah!=200)throw new RuntimeException("请求url失败"); out.close(); con.disconnect(); out.flush(); } catch (Exception e) { e.printStackTrace(); } } } package com.shuangchuan; import java.net.URL; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; public class HttpActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String str[]=new String[]{"yang","wei","dong"}; MyData data=new MyData(); data.post(str); } } package com.shuangchuan; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URL; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class aaaa { // 通过POST将本地数据发送给服务器,string参数表 public static String sendDataByPost(String url, List datas){ org.apache.http.client.HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); HttpResponse resp = null; String result = ""; // post data try { post.setEntity(new UrlEncodedFormEntity(datas,HTTP.UTF_8)); resp = client.execute(post); sendDataByPost(url, datas); result = EntityUtils.toString(resp.getEntity()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档