zoukankan      html  css  js  c++  java
  • Android_post访问java服务器端

    首先又打开android网络权限在androidManifest.x ml中:

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

    //javaee工程访问地址
    String url = "http://localhost:8080/TestAndroid";
    //把要请求的值封装到namevalupair的集合中
    NameValuePair nameValuePair1 = new BasicNameValuePair("name","zhangsna");
    
    NameValuePair nameValuePair2 = new BasicNameValuePair("age","12");
    
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    
     nameValuePairs.add(nameValuePair1);
     nameValuePairs.add(nameValuePair2);
    
    try{
    HttpEntity httpEntity = new UrlEncodedFormEntity(nameValuePairs);
    //通过post请求,把android的值传到javaee工程内处理
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(httpEntity);//将需要提交的数据添加到post请求中
    HttpClient httpClient = new DefaultHttpClient(); //执行post请求 HttpResonse httpResponse = httpClient.execute(httpPost); //httpentity即是请求实体有事返回实体 InputStream = httpEntity.getConteant; //读取客服端返回的值 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    //定义字符串循环出reader内容
      String result = "";
      String line = "";
      while((line= reader.readLine())!=null){
       result = result+line;
      }
    打印服务器返回的内容
    system.out.println(result);
    }
    catche(Exception e){
    }




      

  • 相关阅读:
    html04
    html03
    html02
    html01
    通过脚本获取form表单的数值而不是submit
    myeclipse自带的数据库查看文件
    如何实现数组和List之间的转换?
    Array和ArrayList有何区别?
    ArrayList和LinkedList的区别是什么?
    如何决定使用HashMap还是TreeMap?
  • 原文地址:https://www.cnblogs.com/b422/p/android_post.html
Copyright © 2011-2022 走看看