我想通过 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;
}
}