我会推荐这些教程:
我使用这些教程,并设法得到你想要做的工作,没有太多的困难.
在他们之间,他们描述了如何在每个阶段,Android应用程序,数据库和Web服务器端进行尝试的每个步骤,并提供了额外的信息,然后您可以处理和使用收到的信息
我唯一要添加的是使用PHP和MySql教程的Connect android使用了不推荐使用的php中的mysql_.更好的使用MySqli这是为什么我包括第三个链接.
你想做什么的基本概要是:
1)在android应用程序中,使用类似的类向服务器php脚本发出请求:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
// Response from the HTTP Request
static InputStream httpResponseStream = null;
// JSON Response String to create JSON Object
static String jsonString = "";
// Method to issue HTTP request, parse JSON result and return JSON Object
public JSONObject makeHttpRequest(String url, String method,
List params) {
try {
// get a Http client
DefaultHttpClient httpClient = new DefaultHttpClient();
// If required HTTP method is POST
if (method == "POST") {
// Create a Http POST object
HttpPost httpPost = new HttpPost(url);
// Encode the passed parameters into the Http request
httpPost.setEntity(new UrlEncodedFormEntity(params));
// Execute the request and fetch Http response
HttpResponse httpResponse = httpClient.execute(httpPost);
// Extract the result from the response
HttpEntity httpEntity = httpResponse.getEntity();
// Open the result as an input stream for parsing
httpResponseStream = httpEntity.getContent();
}
// Else if it is GET
else if (method == "GET") {
// Format the parameters correctly for HTTP transmission
String paramString = URLEncodedUtils.format(params, "utf-8");
// Add parameters to url in GET format
url += "?" + paramString;
// Execute the request
HttpGet httpGet = new HttpGet(url);
// Execute the request and fetch Http response
HttpResponse httpResponse = httpClient.execute(httpGet);
// Extract the result from the response
HttpEntity httpEntity = httpResponse.getEntity();
// Open the result as an input stream for parsing
httpResponseStream = httpEntity.getContent();
}
// Catch Possible Exceptions
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
// Create buffered reader for the httpResponceStream
BufferedReader httpResponseReader = new BufferedReader(
new InputStreamReader(httpResponseStream, "iso-8859-1"), 8);
// String to hold current line from httpResponseReader
String line = null;
// Clear jsonString
jsonString = "";
// While there is still more response to read
while ((line = httpResponseReader.readLine()) != null) {
// Add line to jsonString
jsonString += (line + "\n");
}
// Close Response Stream
httpResponseStream.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
// Create jsonObject from the jsonString and return it
return new JSONObject(jsonString);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
// Return null if in error
return null;
}
}
}
它处理通信,打开一个连接并接收一个JSON字符串,然后将其处理成一个JSON对象.
2)在php服务器中,打开一个到你的SQL数据库的mysqli连接,运行一个mysqli-> query()并执行如下结果:
if (mysqli_num_rows($result) > 0) {
// looping through all results
$response["apps"] = array();
while ($row = mysqli_fetch_array($result)) {
$apps = array();
$apps["name"] = $row["name"];
$apps["package"] = $row["package"];
$apps["version"] = $row["version"];
$apps["dateversion"] = $row["dateversion"];
$apps["sdkver"] = $row["sdkver"];
$apps["pathroot"] = $row["pathroot"];
$apps["rootname"] = $row["rootname"];
$apps["apkmd5"] = $row["apkmd5"];
$apps["extraapkmd5"] = $row["extraapkmd5"];
$apps["instructionsmd5"] = $row["instructionsmd5"];
$apps["assetsmd5"] = $row["assetsmd5"];
$apps["root"] = $row["root"];
$apps["current"] = $row["current"];
// push single product into final response array
array_push($response["apps"], $apps);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
这将遍历数据库响应并将其编码为一个JSON字符串,该字符串将发送回Android应用程序,然后处理它.
如何创建这样的东西都在链接的教程中解释