2015-11-19 60 views
3

我目前正在为一个小型项目开发一个Android应用程序,其中包括学习Android平台上的开发。我已经做了所有的应用程序,现在我需要使用MySQL数据库将应用程序中的数据发送到PHP中的服务器。我有一个Web服务,通​​过json格式发送数据,从而在数据库中插入数据。我已经使用Advanced Rest Client测试了Web服务,并且成功地将数据插入到了我的数据库中。现在我从我的Android应用程序应用程序进行测试,但它不起作用。我检查了连接状态,它是“OK”,但是当我检查数据库时,没有插入。通过JSON从Android发送数据到PHP使用HttpUrlConnection的Web服务

下面是PHP我的web服务:

<?php 

$json = file_get_contents('php://input'); 
$obj = json_decode($json); 
$con = mysql_connect('host','login','password') 
    or die('Cannot connect to the DB'); 
mysql_select_db('database_name',$con); 
mysql_query("INSERT INTO `commercial` (firstName, surNameC, email, number, salonName, uuid) 
VALUES ('".$obj->{'firstname'}."','".$obj->{'name'}."','".$obj-> {'email'}."','".$obj->{'phonenumber'}."','".$obj->{'fairreference'}."','".$obj-> {'commercialuuid'}."')"); 
mysql_close($con); 

$posts = "INSERTION OK"; 
header('Content-type: application/json'); 
echo json_encode(array('posts'=>$posts)); 

?> 

而从Android侧发送过程:

URL url; 
    HttpURLConnection urlConnection; 
    StringBuilder strBuilder; 
    try { 
     url = new URL(URL); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setDoOutput(true); 
     urlConnection.setUseCaches(false); 
     urlConnection.setRequestProperty("Content-Type", "application/json"); 
     urlConnection.setReadTimeout(10000); 
     urlConnection.setConnectTimeout(15000); 
     urlConnection.connect(); 

     JSONObject jsonObject = new JSONObject(); 
     jsonObject.put("commercialuuid", args[0]); 
     jsonObject.put("name", args[1]); 
     jsonObject.put("firstname", args[2]); 
     jsonObject.put("phonenumber", args[3]); 
     jsonObject.put("email", args[4]); 
     jsonObject.put("fairreference", args[5]); 

     Log.d("json", jsonObject.toString()); 
     Log.d("request", "starting"); 

     OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream()); 
     writer.write(jsonObject.toString()); 
     writer.flush(); 

     Log.e("connection status",urlConnection.getResponseMessage()); 

     //get the response from the web service 
     InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
     strBuilder = new StringBuilder(); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      strBuilder.append(line); 
     } 
     writer.close(); 
     reader.close(); 
     urlConnection.disconnect(); 
     return null; 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

当我搜索的结果进行了验证,并根据我的工作线程数对此,我真的不明白为什么它不起作用。 如果有人能帮助我,我将不胜感激。

谢谢。

+0

作为第一步,捕获mysql_query()的结果,采取一个厕所k at this:https://github.com/dmnugent80/ShareAppServerSide/blob/master/databaseInsert.php –

+0

感谢您的建议。我做到了,它非常有用。现在我有一些进步,我插入但插入的对象中没有值。 –

+0

我打印了我在Web服务中收到的json,它是空的。所以,问题是我发送json到web服务的方式。有另一种使用outputstreamwriter的方法吗?因为我在Logcat上检查了json,并且很好地创建和格式化了它。 –

回答

2

- >更新: 正如评论所说,这个答案现在已被弃用,你唯一的选择是使用lib Retrofit,即时使用它现在没有问题。寻找它,我推荐使用Retrofit2而不是更旧的版本

您可以使用$交送你只是去的数据要插入

将其插入线程或异步任务

List<NameValuePair> querypair=new ArrayList<NameValuePair>(1); 
         DefaultHttpClient httpClient = new DefaultHttpClient();; 
         querypair.add(new BasicNameValuePair("id", id)); 
         querypair.add(new BasicNameValuePair("name", name)); 
         querypair.add(new BasicNameValuePair("email", email)); 
         HttpPost httpGetquery =new HttpPost("http://www.youradddres/insert.php"); 
         httpGetquery.setEntity(new UrlEncodedFormEntity(querypair)); 
         HttpResponse httpResponseGetQuery = httpClient.execute(httpGetquery); 
         httpEntityQueryGet = httpResponseGetQuery.getEntity(); 
         Log.i("Entity",httpEntityQueryGet.toString()); 

还上传这个PHP

<?php 
$servername = "localhost"; 
$username = "user"; 
$password = "pass"; 
$dbname = "dbname"; 

$con=mysql_connect("localhost","$username","$password"); 
mysql_select_db("$dbname", $con); 

$id=$_POST['id']; 
$name=$_POST['name']; 
$email=$_POST['email']; 

mysql_query("INSERT INTO table(id, name, email) 
VALUES ('$id', '$name', '$email')"); 

echo "New record created successfully"; 

?> 

希望它有帮助

+0

您的DBuser也必须拥有插入权限! –

+0

“DefaultHttpClient”和“BasicNameValuePair”在api 22中被弃用,并在api 23中被删除。 –

+0

是的。现在是JSON在平台之间操纵数据的趋势 –

相关问题