设为首页 收藏本站
查看: 552|回复: 0

[经验分享] apache post 连接网络 读取返回数据

[复制链接]

尚未签到

发表于 2017-1-8 07:18:06 | 显示全部楼层 |阅读模式
  下面是post的:
  public class Test extends Activity implements OnClickListener {
  public Context context;
  private TextView textView1;
  public static String URL ="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
  private DefaultHttpClient httpClient;
  private static final int TIMEOUT = 60;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  init();
  try {
  HttpParams paramsw = createHttpParams();
  httpClient = new DefaultHttpClient(paramsw);
  /* 建立HTTP Post连线 */
  HttpPost post = new HttpPost("http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
  //            URI uri = new URI(URL);
  //            HttpPost post = new HttpPost();
  //            post.setURI(uri);
  // Post运作传送变数必须用NameValuePair[]阵列储存
  List<NameValuePair> params = new ArrayList<NameValuePair>();
  params.add(new BasicNameValuePair("name", "this is post"));
  //            params.add(new BasicNameValuePair("version", getVersionCode(context) + ""));
  //            params.add(new BasicNameValuePair("os", "android"));
  //            params.add(new BasicNameValuePair("flag", "10"));
  //            params.add(new BasicNameValuePair("modle", getModelNumber()));
  //            params.add(new BasicNameValuePair("channel", "samsung"));
  try {
  // 发出HTTP request,把字符集设置在request里面
  post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
  //相当于打开链接,给出回应
  HttpResponse httpResponse = httpClient.execute(post);
  int httpCode = httpResponse.getStatusLine().getStatusCode();
  //HTTP_OK=200,服务器作出反映,找到了服务器,并且回应了。
  if (httpCode == HttpURLConnection.HTTP_OK) {
  //                  //取出回应字串
  //                    String strResult=EntityUtils.toString(httpResponse.getEntity());
  //                    textView1.setText(strResult);
  //这里可以做很多事 
  Header[] headers=httpResponse.getAllHeaders();
  HttpEntity entity=httpResponse.getEntity();
  Header header=httpResponse.getFirstHeader("content-type");
  //下面是从流中把字符串读出来了
  InputStream inputStream=entity.getContent();
  InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
  BufferedReader reader = new BufferedReader(inputStreamReader);//读字符串用的。
  String inputLine = null;
  String result = "";
  // 使用循环来读取获得的数据,把数据都村到result中了
  while (((inputLine = reader.readLine()) != null)) {
  // 我们在每一行后面加上一个"\n"来换行
  result += inputLine + "\n";
  }
  reader.close();//关闭输入流
  }else {
  textView1.setText("Error Response"+httpResponse.getStatusLine().toString());
  }
  } catch (UnsupportedEncodingException e) {
  textView1.setText("网络连接错误UnsupportedEncodingException");
  } catch (ClientProtocolException e) {
  textView1.setText("网络连接错误ClientProtocolException");
  } catch (IOException e) {
  textView1.setText("网络连接错误IOException");
  }
  } finally {
  if (httpClient != null) {
  httpClient.getConnectionManager().shutdown();//最后关掉链接。
  httpClient = null;
  }
  }
  }
  /**
  * 手机型号
  * 
  * @return
  */
  private String getModelNumber() {
  String modelNo = "Unknown";
  try {
  modelNo = Build.MODEL;
  } catch (Exception e) {
  // ignored
  }
  return modelNo;
  }
  /**
  * 获得版本CODE
  * 
  * @param context
  * @return
  */
  public static int getVersionCode(Context context) {
  PackageManager pm = context.getPackageManager();
  try {
  PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
  if (info != null) {
  return info.versionCode;
  }
  } catch (NameNotFoundException e) {
  e.printStackTrace();
  }
  return 1;
  }
  /**
  * Create the default HTTP protocol parameters.
  */
  public static final HttpParams createHttpParams() {
  final HttpParams params = new BasicHttpParams();
  // Turn off stale checking. Our connections break all the time anyway,
  // and it's not worth it to pay the penalty of checking every time.
  HttpConnectionParams.setStaleCheckingEnabled(params, false);
  //设置链接超时,socket超时,以及socket大小。
  HttpConnectionParams.setConnectionTimeout(params, TIMEOUT * 1000);
  HttpConnectionParams.setSoTimeout(params, TIMEOUT * 1000);
  HttpConnectionParams.setSocketBufferSize(params, 8192 * 5);
  return params;
  }
  private void init() {
  // TODO Auto-generated method stub
  textView1 = (TextView) findViewById(R.id.txt);
  Button button1 = (Button) findViewById(R.id.button1);
  Button button2 = (Button) findViewById(R.id.button2);
  Button button3 = (Button) findViewById(R.id.button3);
  button1.setOnClickListener(this);
  button2.setOnClickListener(this);
  button3.setOnClickListener(this);
  }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-325255-1-1.html 上篇帖子: apache james 发送邮件 保存 数据库 下篇帖子: apache httpget 网络连接 读取数据
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表