在Eclipse中连接Android与WAMP服务器

发布于 2024-12-18 17:22:44 字数 3998 浏览 0 评论 0原文

您好,我是 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" 

并遵循了其他几个有效的示例,但没有成功。

我真的很感激任何能帮助我解决这个问题的帮助。

Hi I am newbie to Android development and in particular to connecting to servers from apps. I am currently attempting to connect my android app to a database on a WAMP server. I hope in the future to use this as a means to login but for the mean time I am just looking to retrieve the data that I have stored within the database. I am really confused with all of this at the moment and have looked at numerous tutorials and my code looks to be the exact same as the code which is posted within all of the tutorials and I have followed all of the steps.

I would be very grateful to anyone out there who could help me with this as I am truely stuck on this and unable to find a solution to my problem.

Thanks very much. Please find below my code..

    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);

    }

}

I have included the line:

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

and have followed several other worked examples yet with no success.

I really would appreciate any help that would aid me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泅人 2024-12-25 17:22:44

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

It looks like you are pointing to an external IP address. Do you have the port forwarding and everything set up correctly on your home network? Maybe you should set up a simple HTML page to test if you can at least access it from the outside without issue.

傲世九天 2024-12-25 17:22:44

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

Hey It seems there is no problem with code may be the problem with Wamp server configuration connection make sure your Wamp server is working properly and just go to any internet browser and hit the localhost url and try your(localhost/wamp/www/Food/food.php) Url also..

想你的星星会说话 2024-12-25 17:22:44

将以下行: 替换

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)");

Replace the following line:

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

with this line:

HttpPost httppost = new HttpPost("[http://10.0.2.2/Food/food.php](LINK)");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文