1

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

}

4

1 回答 1

2

这是尝试在本地连接。

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 您将不再连接到此路由器。您将需要以另一种方式托管您的文件...通过设置服务器、通过网络托管服务托管您的文件等...以便远程访问文件!

于 2012-07-13T18:04:07.580 回答