u012731248
Sharon1412
2017-06-08 12:58

Android studio使用HttpurlConnection无法连接的问题。

  • 线程
  • android-studio
  • 服务器
  • web service

背景:Android studio编写获取webservice soap xml 我在eclipse中用java编写的个测试例子,
用的是HttpurlConnection可以成功读取数据,但是我搬到Android studio中,无法连接,会跳转到catch。我也按照网上说的方法加了线程,以及httpclient不能使用情况下载build中加入相关uselibary等等方法,都无法连接成功。请问我应该怎么解决,有懂的朋友帮帮忙,谢谢。
以下是代码:
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){

                   new Thread(new Runnable()
                    {
                       @Override
                         public void run()
                          {   String id= edit.getText().toString();
                              httpClient(id);

                              }
                       }).start(); //这段代码在主线程中调用,开启一个线程
                   // String id= edit.getText().toString();
                    // httpClient(id);


             }


    });

}

private  void httpClient(String id) {

    //以SOAP1.1的格式发送
    String httpBody = XMLDom.requestSOAP1(id);
    myXMLPost(httpBody);
 }
  private void myXMLPost(final String httpBody) {

              Map<String, String> map1 = new HashMap<String, String>();
              try {//创建post对象
                  HttpURLConnection connection = (HttpURLConnection) new URL("http://119.10.51.138:86/erp.asmx").openConnection();
                  connection.setDoOutput(true);
                  connection.setDoInput(true);
                  connection.setRequestMethod("POST");
                  connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
                  connection.setRequestProperty("SOAPAction", "http://tempuri.org/GetPROC_ORDER_Detail");
                  connection.connect();//连接服务器
                  OutputStream os = (OutputStream) connection.getOutputStream();
                  os.write(httpBody.getBytes());
                  os.flush();
                  os.close();
                  InputStream is = (InputStream) connection.getInputStream();//获取数据,真正的请求从此处开始
                  byte[] bts = new byte[is.available()];
                  is.read(bts);
                  String detailResult = new String(bts);
                  map1 = XMLDom.getLoginResult(detailResult);
                  String s = map1.get("sl");
                  result.setText(s);
              } catch (Exception e) {
                  e.printStackTrace();
                  result.setText("连接错误");
              }
          }
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换