Android 从自己的网站获取数据的介绍或教程?
我需要在我的Android应用程序中实现一个模块,从我自己的网站获取一些文本内容(不是显示在浏览器上,而是由应用程序处理) 文本内容将存储在数据库(MySQL?)中 我唯一的问题是我以前从未做过类似的事情(尽管我熟悉基本SQL),所以我不确定什么是解决这个问题的好方法,我甚至不知道从哪里开始 我是否应该从头创建一个MySQL数据库并开始围绕它编写PHP代码 是否有一个框架或包装器已经面向与Android应用程序通信,并让我专注于我需要的特性Android 从自己的网站获取数据的介绍或教程?,android,Android,我需要在我的Android应用程序中实现一个模块,从我自己的网站获取一些文本内容(不是显示在浏览器上,而是由应用程序处理) 文本内容将存储在数据库(MySQL?)中 我唯一的问题是我以前从未做过类似的事情(尽管我熟悉基本SQL),所以我不确定什么是解决这个问题的好方法,我甚至不知道从哪里开始 我是否应该从头创建一个MySQL数据库并开始围绕它编写PHP代码 是否有一个框架或包装器已经面向与Android应用程序通信,并让我专注于我需要的特性 您推荐什么作为一个好的起点?我最近调用了一个.Net
您推荐什么作为一个好的起点?我最近调用了一个.Net web服务来检索我的信息。我从android应用程序中使用KSoap2调用了web服务。在四处搜索的过程中,我找不到任何使之更容易的框架。最后事实证明这很容易。以下是您的出发点:
本教程有帮助吗 这可以通过多种方式实现,你可以做一些非常简单的事情,但随着你的进步,并开始将更多信息从服务器发送到你的应用程序,你需要实现一些更强大的功能 虽然@robertanswer是有效的,但我相信对于手机来说,使用JSON的restfulwebservices要好得多 您可以根据需要对Web服务进行编程,我建议您(如果您可以使用java进行服务器端编程)看看哪一个库是使用JSON的最佳库。通过使用JSON对象,您可以将所有需要的信息转换为字符串,然后让手机将其转换为Java对象。您可以用它做任何您想做的事情,无论它是在UI中显示还是存储在数据库中
<?php
$link = mysqli_connect("localhost", "dbuser", "dbpassword", "database");
$response = array();
$pass = mysqli_query("select * from table");
while($data = mysqli_fetch_object($pass))
{
array_push($response, array(
"my_field" => $data->my_field
));
}
echo json_encode($response);
mysqli_close($link);
当应用程序通过HTTP POST或GET响应请求数据时,您将使用库解析答案。因为我在PHP端使用JSON,所以我目前在我的项目中使用JSON
要从Android应用程序请求JSON数据,可以使用我在某处(不记得了)带的RestClass,但代码如下:
public class RestClient
{
private ArrayList <NameValuePair> params;
private ArrayList <NameValuePair> headers;
private String url;
private int responseCode;
private String message;
private String response;
public enum RequestMethod
{
GET,
POST
}
public String getResponse() {
return response;
}
public String getErrorMessage() {
return message;
}
public int getResponseCode() {
return responseCode;
}
public RestClient(String url)
{
this.url = url;
params = new ArrayList<NameValuePair>();
headers = new ArrayList<NameValuePair>();
}
// -------------------------------------------------------------------------------
// Various methods.
// -------------------------------------------------------------------------------
/**
* Add a parameter to the request (pair, name/value).
* @param name Name of the parameter (ex: &name=)
* @param value Value of the parameter (ex: &name=value).
*/
public void AddParam(String name, String value)
{
params.add(new BasicNameValuePair(name, value));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Add a header to the request (if needed).
* @param name Name of the header. (ex: Content-Type)
* @param value Value of the header. (ex: Content-Type = text/html).
*/
public void AddHeader(String name, String value)
{
headers.add(new BasicNameValuePair(name, value));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Fetch the HTTP content via POST or GET.
* @param method Method to be used.
* @throws Exception Exception to be catched in case of a network problem.
*/
public void Execute(RequestMethod method) throws Exception
{
switch(method) {
case GET:
{
//add parameters
String combinedParams = "";
if(!params.isEmpty()){
combinedParams += "?";
for(NameValuePair p : params)
{
String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");
if(combinedParams.length() > 1)
{
combinedParams += "&" + paramString;
}
else
{
combinedParams += paramString;
}
}
}
HttpGet request = new HttpGet(url + combinedParams);
//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}
executeRequest(request, url);
break;
}
case POST:
{
HttpPost request = new HttpPost(url);
//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}
if(!params.isEmpty()){
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
executeRequest(request, url);
break;
}
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Alternative method to execute the HTTP request.
* @param request The HTTP request object to be used.
* @param url HTTP url.
*/
private void executeRequest(HttpUriRequest request, String url)
{
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Private method. Convert an HTTP stream to a string stream.
* @param is InputStream to be converted.
* @return String to be used.
*/
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
瞧!您可以使用JSON数据,这些数据是通过Android应用程序中的PHP脚本创建的。别忘了将android.permission.INTERNET添加到您的AndroidManifest.xml中。否则,应用程序将无法与PHP脚本通信
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
祝你好运。谢谢。与此同时,我发现了这个。也很有帮助。@uTubeFan谢谢分享。我得看一下。谢谢你的回答。非常感谢你的详细回答。我打算从介绍Restful Web服务(以前从未听说过这个术语)开始,然后深入研究实际代码。这很有帮助,谢谢!请随意评论或提出其他问题。我们是来帮你的。关门了???为什么?至少有4个人觉得这个问题很容易回答,他们回答得很好,而且非常有帮助。
RestClient c = new RestClient("http://www.myserver.com/json.php");
c.AddParam("my_field_value", 1);
try
{
c.Execute(RequestMethod.POST);
// Here you can parse the JSON with an instance of Gson class.
Gson jsonObject = new Gson();
String jsonOutput = jsonObject.toJson(c.getResponse());
// Do whatever you need with the data.
}
catch (Exception e)
{
e.printStackTrace();
}
<uses-permission android:name="android.permission.INTERNET"></uses-permission>