douzhuo6270 2014-10-09 19:32
浏览 10

如何在Android上发出与此PHP代码相同的请求?

I am new to both PHP and Android.

I am doing this request like this in PHP which works well. The "name" will contain a variable like "upperLight" so that we can compare with the android code.

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_connect($sock,"192.168.2.104", 80);

$msg = 'a';
if (isset($_POST["name"])){
    $msg= $_POST["name"] ;
}

socket_write($sock, $msg);

I am trying to do the same in android like this but its not working.

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("upperLight","1"));

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URLTest);
        post.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse execute = client.execute(post);

On the other end (192.168.2.104) i am checking for the word "upperLight" in the request.

EDIT : the variable URLTest contains - "192.168.2.104:80"

  • 写回答

2条回答 默认 最新

  • doushan15559 2014-10-09 20:21
    关注
    • try this

      URL url = new URL("http://192.168.2.104:80");
      URLConnection con = url.openConnection();
      con.setDoOutput(true);
      OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
      out.write( "upperLight" );
      out.flush();
      
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器