gpt4 book ai didi

android - 通过 Android 3g 连接到服务器并从 phpmyadmin 检索数据

转载 作者:行者123 更新时间:2023-11-29 22:03:04 24 4
gpt4 key购买 nike

我想通过 3G 从我的 Android 创建到服务器的连接,但我没有成功。如果 Android 手机通过 wifi 连接并且它与我的服务器是同一个 wifi,我就成功了。你能帮我吗,给我一段代码来做这件事?

这是我的代码,首先是请求我的数据库的 php:

<?php
// on se connecte à notre base pour recuperer les data
$base = mysql_connect ('localhost', 'root', '');
mysql_select_db ('routelibre', $base) ;
$variable = $_GET['variable'];
$first_token = strtok($variable, ";;");
$second_token = strtok(";;");
$req =mysql_query("SELECT message from messages where departement='$first_token' AND voie='$second_token'");
$output=array();
while ($row=mysql_fetch_array($req)) {
$output[]=$row;
}
//on encode en JSON
print(json_encode($output));
mysql_free_result ($req);
?>

这是安卓的代码:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Context;
import android.net.ParseException;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.content.Context;


public class ConnexionSQL {
public void ConnexionSQLActivity(){};

public ArrayList<String> ConnexionBD(String dataFromHMI, Context context) {
String result = null;
InputStream is = null;
JSONObject json_data=null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
ArrayList<String> lesMessages = new ArrayList<String>();

StrictMode.ThreadPolicy policy = new

StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);

try{
//commandes httpClient
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new

HttpPost("http://192.168.1.1:80/connexion_mysql.php?variable=" + dataFromHMI);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.i("taghttppost",""+e.toString());
Toast.makeText(context,e.toString() ,Toast.LENGTH_LONG).show();
}


//conversion de la réponse en chaine de caractère
try
{
BufferedReader reader = new BufferedReader(new

InputStreamReader(is,"UTF-8"));

StringBuilder sb = new StringBuilder();

String line = null;

while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}

is.close();

result = sb.toString();
}
catch(Exception e)
{
Log.i("tagconvertstr",""+e.toString());
}
//recuperation des donnees json
try{
JSONArray jArray = new JSONArray(result);

for(int i=0;i<jArray.length();i++)
{

json_data = jArray.getJSONObject(i);
lesMessages.add(json_data.getString("message"));
//r.add(json_data.getString("categorie"));

}
}
catch(JSONException e){
Log.i("tagjsonexp",""+e.toString());
} catch (ParseException e) {
Log.i("tagjsonpars",""+e.toString());
}
return lesMessages;
}

最佳答案

这是尝试本地连接。

HttpPost("http://192.168.1.1:80/connexion_mysql.php?variable=" + dataFromHMI);

您的代码是否位于可以在未连接到本地计算机时访问的服务器上?这是您需要发布到的网址!

192.168.1.1 是 Linksys 路由器(可能还有其他路由器?)使用的默认 IP 地址。参见:http://www.tech-faq.com/192-168-1-1.html

因此,当您通过 wifi 连接到此路由器时,您可以访问存储在@192.168.1.1:80 但超过 3g 的源,您将不再连接到此路由器。您将需要以另一种方式托管文件...通过设置服务器、通过网络托管服务托管文件等...以便远程访问文件!

关于android - 通过 Android 3g 连接到服务器并从 phpmyadmin 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11471195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com