android新手

ANDROID_未羊 2013-10-29 01:17:00
做了个web服务端,如何用android客户端交互数据
...全文
97 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiuxin_qx 2013-10-29
  • 打赏
  • 举报
回复
引用 2 楼 book_jb9802 的回复:
public static String doPostJson(String urlStr, Map<String, String> heads, String jsonStr) { try{ URL url = new URL(urlStr); HttpURLConnection connection = ((HttpURLConnection) url.openConnection()); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setConnectTimeout(5000); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset", "UTF-8"); //connection.setRequestProperty("Content-Type", "application/json-rpc"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); if (heads != null){ for(Map.Entry<String, String> head : heads.entrySet()){ connection.setRequestProperty(head.getKey(), head.getValue()); } } byte[] sendData = jsonStr.getBytes("UTF-8"); connection.setFixedLengthStreamingMode(sendData.length); connection.connect(); OutputStream out = connection.getOutputStream(); //out.write(sendData, 0, sendData.length); out.write(sendData); out.flush(); out.close(); InputStream input = connection.getInputStream(); StringBuffer sb = new StringBuffer(); byte[] buffer = new byte[512]; int len; while ((len = input.read(buffer)) != -1) { sb.append(new String(buffer, 0, len)); } input.close(); connection.disconnect(); return sb.toString(); }catch(Exception e){ Log.e("System.out", Log.getStackTraceString(e)); } return ""; } /** * 执行一个Http Get 请求,返回请求响应的HTML * @param url * 请求的URL地址 * @param heads * 请求时要设置的http头信息,没有设为null * */ public static String doGet(String url, Map<String, String> heads) { HttpResponse httpResponse = null; try { // 第1步:创建HttpGet对象 HttpGet httpGet = new HttpGet(url); if (heads != null){ for(Map.Entry<String, String> head : heads.entrySet()){ httpGet.addHeader(head.getKey(), head.getValue()); } } // 第2步:使用execute方法发送HTTP POST请求,并返回HttpResponse对象 httpResponse = new DefaultHttpClient().execute(httpGet); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == 200) { // 第3步:使用getEntity方法获得返回结果 String result = EntityUtils.toString(httpResponse.getEntity()); //System.out.println(result); return result; } } catch (Exception e) { Log.e("System.out", "Exception: "+Log.getStackTraceString(e)); } return null; }
这个还可以!可以试试
mmorss 2013-10-29
  • 打赏
  • 举报
回复
引用 3 楼 q343524926 的回复:
http://hi.baidu.com/yezhonglu/item/ad7b6aa6b1cb021a030a4d37
楼主可以参考一下
hisadqw 2013-10-29
  • 打赏
  • 举报
回复
http://hi.baidu.com/yezhonglu/item/ad7b6aa6b1cb021a030a4d37 楼主可以参考一下
book_jb9802 2013-10-29
  • 打赏
  • 举报
回复
public static String doPostJson(String urlStr, Map<String, String> heads, String jsonStr) { try{ URL url = new URL(urlStr); HttpURLConnection connection = ((HttpURLConnection) url.openConnection()); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setConnectTimeout(5000); connection.setRequestMethod("POST"); connection.setRequestProperty("Charset", "UTF-8"); //connection.setRequestProperty("Content-Type", "application/json-rpc"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); if (heads != null){ for(Map.Entry<String, String> head : heads.entrySet()){ connection.setRequestProperty(head.getKey(), head.getValue()); } } byte[] sendData = jsonStr.getBytes("UTF-8"); connection.setFixedLengthStreamingMode(sendData.length); connection.connect(); OutputStream out = connection.getOutputStream(); //out.write(sendData, 0, sendData.length); out.write(sendData); out.flush(); out.close(); InputStream input = connection.getInputStream(); StringBuffer sb = new StringBuffer(); byte[] buffer = new byte[512]; int len; while ((len = input.read(buffer)) != -1) { sb.append(new String(buffer, 0, len)); } input.close(); connection.disconnect(); return sb.toString(); }catch(Exception e){ Log.e("System.out", Log.getStackTraceString(e)); } return ""; } /** * 执行一个Http Get 请求,返回请求响应的HTML * @param url * 请求的URL地址 * @param heads * 请求时要设置的http头信息,没有设为null * */ public static String doGet(String url, Map<String, String> heads) { HttpResponse httpResponse = null; try { // 第1步:创建HttpGet对象 HttpGet httpGet = new HttpGet(url); if (heads != null){ for(Map.Entry<String, String> head : heads.entrySet()){ httpGet.addHeader(head.getKey(), head.getValue()); } } // 第2步:使用execute方法发送HTTP POST请求,并返回HttpResponse对象 httpResponse = new DefaultHttpClient().execute(httpGet); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == 200) { // 第3步:使用getEntity方法获得返回结果 String result = EntityUtils.toString(httpResponse.getEntity()); //System.out.println(result); return result; } } catch (Exception e) { Log.e("System.out", "Exception: "+Log.getStackTraceString(e)); } return null; }
小夏白糖 2013-10-29
  • 打赏
  • 举报
回复
使用json吧

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧