php服务器端与android客户端通信

服务器端:

<?php
// array for JSON response
var_dump(json_decode($json));
$response = array();
include("conn.php");
// check for required fields
if (isset($_POST['name'])) {

$name = $_POST['name'];

$result = mysql_query("INSERT INTO device_info(date) VALUES('$name')");
// $result= mysql_query("select * from device_info where date='$name'");
echo $result;

// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";

// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";

// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

 

android端:

package wang.test;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class up extends Activity {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputName;
private static String url_up = "http://192.168.137.1/test/communicate.php";
private static final String TAG_MESSAGE = "message";


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.up);
inputName = (EditText) findViewById(R.id.inputName);
Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {


public void onClick(View view) {
// creating new product in background thread
if(validate()){
new Up().execute();
}
}
});

}
private boolean validate()
{
String description = inputName.getText().toString().trim();
if (description.equals(""))
{
DialogUtil.showDialog(this, "您还没有填写建议", false);
return false;
}

return true;
}
class Up extends AsyncTask<String, String, String> {


/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(up.this);
pDialog.setMessage("验证中..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputName.getText().toString();


// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));


// getting JSON Object
// Note that create product url accepts POST method
try{
JSONObject json = jsonParser.makeHttpRequest(url_up,
"POST", params);
String message = json.getString(TAG_MESSAGE);
return message;
}catch(Exception e){
e.printStackTrace();
return "";
}
// check for success tag


}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String message) {
pDialog.dismiss();
//message 为接收doInbackground的返回值
Toast.makeText(getApplicationContext(), message, 8000).show();


}
}
}

转载于:https://www.cnblogs.com/FanTasYX/archive/2013/03/28/2987194.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值