2016-09-20 79 views
0

我想从我的android工作室应用程序发送数据到MySQL的PHP​​服务器,这里是我的代码,该程序可以运行,但我的服务器无法从我的应用程序接收数据。我的编码在某个地方出错了吗?从Android工作室应用程序发送数据到服务器

我的Android应用程序的工作室编码

package com.sp.toilert; 

import android.app.PendingIntent; 
import android.bluetooth.BluetoothAdapter; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.nfc.NdefMessage; 
import android.nfc.NdefRecord; 
import android.nfc.NfcAdapter; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
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 java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 


public class MainActivity extends AppCompatActivity { 

NfcAdapter nfcAdapter; 
TextView txtTagContent; 
private BluetoothAdapter myBluetooth = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(com.sp.toilert.R.layout.activity_main); 

    checkBTState(); 
    nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    txtTagContent = (TextView) findViewById(R.id.txtTagContent); 

} 

public void buttonOnClick(View v) { 

new SubmitData().execute(); //do something when button clicked 

} 

public class SubmitData extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... strings) { 

     try { 

      postData(); 

     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    //this function is the main thing that will upload the data to the server 
    public void postData() { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://172.22.47.245/notthatdirty.php"); 

     try { 
      // these are all the fields that will be submitted 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      //nameValuePairs.add(new BasicNameValuePair("MyName","MyValue")); 
      nameValuePairs.add(new BasicNameValuePair("cubicle_ID", "100003")); //send data to server 
      nameValuePairs.add(new BasicNameValuePair("issue", "dirty")); //send data to server 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 
    } 
} 

我的服务器编码

<h1>WELCOME TO TOILERT</h1></P> 

<?php 
$servername = "localhost"; 
$username = "toilert"; 
$password = "toilert"; 
$dbname = "cleaning_db"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 

// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 
echo "Connected successfully"; 

//http://localhost/toilet-backend/backend.php?dirty=1&cubicleID=10003 
//GET the data from android 
/* 
$dirty = $_GET["dirty"]; //1 for true . 0 for false 
$emergency = $_GET["emergency"]; //1 for true . 0 for false 
$faulty = $_GET["faulty"]; //1 for true . 0 for false 
*/ 

$issue = htmlspecialchars($_GET["issue"]); // Sanitize input with   htmlspecialchars 
$cubicleID = $_GET['cubicleID']; 

//Check if the request has been sent before 
//ensure no duplicate toilet data 
$query = "SELECT * FROM cleaner_request WHERE cubicle_id = $cubicleID"; 
$result = $conn->query($query); 

if ($result->num_rows > 0) { 
$json = array("status" => 200, "msg" => "Complaint has already been   created"); 
echo json_encode($json); 
} else { 
switch ($issue) { 
    case "dirty": 
    case "emergency": 
    case "faulty": 
     $query = "INSERT INTO cleaner_request (cubicle_id, $issue) VALUES  ('$cubicleID', '1')"; 
     break; 
    default: 
     $error = "Invalid status"; 
} 
if (empty($error)) { 
    $result = $conn->query($query); 
    echo "Data written to Database"; 
    $json = array("status" => 200, "msg" => "Complaint has been successfully created"); 
    } else { 
    echo "error" ; 
    //$json = array("status" => 418, "msg" => $error); 

} 
echo json_encode($json); 
/* Ben commented this out. Because code 
if ($dirty == 1) { //dirty 
    $query = "INSERT INTO cleaner_request (cubicle_id,dirty) VALUES ('$cubicleID','$dirty')"; 
    $result = $conn->query($query); 
    $json = array("status" => 200, "msg" => "Complaint has been successfully created"); 
    echo json_encode($json); 
    } else if ($dirty == 0) { //clean 

    } 
    */ 
    //check require location 
    require 'C:/xampp/htdocs/toilet-backend/twilio-php-master/Services/Twilio.php'; 
$AccountSid = "AC52a80f076cca00c9547fdfd17bf4fbbb"; 
$AuthToken = "b145c2d4f7b722fe79cbe38f3009afcc"; 

// Step 3: instantiate a new Twilio Rest Client 
$client = new Services_Twilio($AccountSid, $AuthToken); 
$client->account->messages->create(array(
    'To' => "+65 9067 9919", 
    'From' => "+15672420325", 
    'Body' => "Greetings, please clean up Cubicle 100 001 and 100 004. Thank you. This service is brought to you by Toilert.", 
)); 
mysqli_close($conn); //$conn->close(); 
} 
?> 

A picture of the server side

回答

0

https://developer.android.com/training/volley/index.html

停止强调,并使用排它为我工作得很好。

Volley是一个HTTP库,可以使Android应用程序的联网变得更简单,最重要的是,更快。 Volley通过开放的AOSP存储库提供。

Volley提供以下好处:

自动调度网络请求。 多个并发网络连接。 标准HTTP缓存一致性的透明磁盘和内存响应缓存。 支持请求优先级。 取消请求API。您可以取消单个请求,也可以设置要取消的请求的范围或范围。 易于定制,例如,重试和退避。 强大的排序使得使用从网络异步获取的数据可以轻松正确地填充您的UI。 调试和跟踪工具。

相关问题