我的android和php代码出了什么问题。每当我从android传递数据时,android代码中都没有显示错误,但在php中的mysql仍然没有我从android收到的数据。
这是android的代码。
package com.example.datatoserver;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
StringBody tname, tdetails, tcategory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView name = (TextView) findViewById(R.id.mainName);
TextView details = (TextView) findViewById(R.id.mainDetails);
TextView category = (TextView) findViewById(R.id.mainCategory);
Button submit = (Button) findViewById(R.id.mainSubmit);
tname = new StringBody(name.getText().toString(),
ContentType.TEXT_PLAIN);
tdetails = new StringBody(details.getText().toString(),
ContentType.TEXT_PLAIN);
tcategory = new StringBody(category.getText().toString(),
ContentType.TEXT_PLAIN);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
StringBody[] params = { tname, tdetails, tcategory };
new uploadToInternet().execute(params);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("conenction", "connection error: " + e.toString());
e.printStackTrace();
}
}
});
}
}
class uploadToInternet extends AsyncTask<StringBody, Void, Void> {
@Override
protected Void doInBackground(StringBody... params) {
try {
upload(params[0], params[1], params[2]);
Log.d("success 2", "success");
}
catch (Exception e) {
Log.d("error", "" + e.toString());
}
return null;
}
private void upload(StringBody name, StringBody details, StringBody category)
throws Exception {
String url = "http://192.168.1.101/android_connect2/create_report.php";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// post.addHeader("Content-Type", "multipart/form-data");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("name", name);
builder.addPart("details", details);
builder.addPart("category", category);
post.setEntity(builder.build());
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String t = EntityUtils.toString(response.getEntity());
Log.d("status :", response.getStatusLine().toString());
Log.d("return :", t);
entity.consumeContent();
client.getConnectionManager().shutdown();
Log.d("success", "success");
}
}
这是我的php服务器端代码,它接收来自android的数据
<?php
$response = array();
if(isset($_POST['name'])&& isset($_POST['details'])&&isset($_POST['category'])) {
$name=$_POST['name'];
$details=$_POST['details'];
$category=$_POST['category'];
require_once'db_connect.php';
$db = new DB_CONNECT();
$result = ("INSERT INTO reports(name, details, category) VALUES('$name', '$details', '$category')");
}
?>
这里是数据库连接php代码
<?php
class DB_CONNECT{
function __constructor(){
$this->connect();
}
function __destructor(){
$this->close();
}
function __connect(){
require_once '/db_config.php';
$con = mysql_connect(DB_SERVER , DB_USER, DB_PASSWORD) or die (mysql_error());
$db = mysql_select_db(DB_DATABASE)or die (mysql_error()) or die (mysql_error());
return $con;
}
function close()
{
mysql_close();
}
}
?>
我真的不知道为什么我没有收到数据进入我的数据库。我确定php代码是正确的,所以android?我希望你的回复。提前谢谢。