从Android发送HTTP请求到PHP

时间:2014-07-26 15:32:51

标签: php android http arduino

我正在尝试使用以下代码从Android向PHP发送HTTP请求,但应用程序在启动时会一直崩溃。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_android_test);

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://192.168.1.5:80/arduino2/socket.php");
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("lighton", ""));
    try {
        post.setEntity(new UrlEncodedFormEntity(pairs));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    try {
        HttpResponse response = client.execute(post);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是我正在尝试发送请求的PHP(socket.php)文件。这个PHP文件向Arduino发送请求,当使用HTML按钮并提交表单时,该请求工作正常。

<?php 
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock,"192.168.1.8", 80);

$msg = 'a';
if (isset($_POST['lighton'])){
    $msg='lighton';
}
if (isset($_POST['lightoff'])){
    $msg='lightoff';
}
if (isset($_POST['relayoff'])){
    $msg='relayoff';
}
if (isset($_POST['relayon'])){
    $msg='relayon';
}
if (isset($_POST['temp'])){
    $msg='temp';
}
echo $sock;
socket_write($sock, $msg);
sleep(1);
header("Location: http://192.168.1.5/arduino2/index.php"); /* Redirect browser */
exit();
?>

1 个答案:

答案 0 :(得分:0)

您应该使用asynctask进行网络操作。这是代码:

AsyncTask类

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.AsyncTask;

public class MyNetworkOperation extends AsyncTask<Void, Void, Void> {

private String url;

// Pass your url with constructor
public MyNetworkOperation(String url) {
    this.url = url;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected Void doInBackground(Void... params) {
    // Do your network operations like post, get, download, upload etc.
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("lighton", ""));
    try {
        post.setEntity(new UrlEncodedFormEntity(pairs));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    try {
        HttpResponse response = client.execute(post);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
}
}

你怎么称呼这个电话?看看下面的

MyNetworkOperation asynctask = new MyNetworkOperation("http://192.168.1.5:80/arduino2/socket.php");
asynctask.execute();