将Eclipse中的Android与WAMP服务器连接

时间:2011-11-27 13:58:35

标签: php android eclipse wamp

您好我是Android开发的新手,特别是从应用程序连接到服务器。我目前正在尝试将我的Android应用程序连接到WAMP服务器上的数据库。我希望将来能够使用它作为一种登录手段,但同时我只想查找我存储在数据库中的数据。我现在对所有这些感到困惑,看了很多教程,我的代码看起来与所有教程中发布的代码完全相同,我已经按照所有步骤进行了操作。

我会非常感谢那些可以帮助我解决这个问题的人,因为我真的坚持这个并且无法找到问题的解决方案。

非常感谢。请在下面找到我的代码..

    package eoghan.android;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;

    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.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.Activity;
    import android.net.ParseException;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;

    public class FoodPHPActivity extends Activity {



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String result = null;
        InputStream is = null;
        StringBuilder sb=null;

        //http post
        try{
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost("http://10.0.2.2/wamp/www/Food/food.php");

              List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
              nameValuePairs.add(new BasicNameValuePair("Food_Name", "Apple"));  

              httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
              HttpResponse response = httpclient.execute(httppost); 
              HttpEntity entity = response.getEntity();
              is = entity.getContent();
              Log.e("log_tag", "connection success ");
              Toast.makeText(getApplicationContext(), "pass 123 ", Toast.LENGTH_SHORT).show();

        }catch(Exception e){
            Log.e("log_tag", "Error in http connection"+e.toString());
        }

        //convert response to string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line="0";

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

            is.close();

            result=sb.toString();

        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }
        //parsing data
        int fd_id = 0;
        String fd_name = null;

        try{
            System.out.println("Entering the try method to connect the JSON");

            JSONArray jArray = new JSONArray(result);

            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++){
                    json_data = jArray.getJSONObject(i);
                    fd_id=json_data.getInt("Food_ID");
                    fd_name=json_data.getString("Food_Name");
                    Toast.makeText(getBaseContext(), "JSON works", Toast.LENGTH_LONG).show();

        }

        }
        catch(JSONException e1){
            Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show();
        }

        catch (ParseException e1){
            e1.printStackTrace();
         }

        System.out.println(fd_id);
        System.out.println(fd_name);

    }

}

我已将该行包括在内:

uses-permission android:name="android.permission.INTERNET" 

并且已经跟踪了其他一些有用的例子但没有成功。

我真的很感激能帮到我的任何帮助。

3 个答案:

答案 0 :(得分:0)

看起来您正指向外部IP地址。您的家庭网络上是否有正确的端口转发和所有设置?也许您应该设置一个简单的HTML页面来测试您是否至少可以从外部访问它而不会出现问题。

答案 1 :(得分:0)

嘿似乎没有问题代码可能是Wamp服务器配置连接的问题确保你的Wamp服务器工作正常,只需去任何互联网浏览器并点击localhost网址并尝试你的(localhost / wamp / www) /Food/food.php)网址也.. ..

答案 2 :(得分:0)

替换以下行:

HttpPost httppost = new HttpPost("[http://10.0.2.2/wamp/www/Food/food.php](LINK)");

这一行:

HttpPost httppost = new HttpPost("[http://10.0.2.2/Food/food.php](LINK)");