簡體   English   中英

如何在android中的localhost上測試簡單的后端程序?

[英]How to test simple backend program on localhost in android?

我做了一個非常簡單的后端程序(它只發送簡單的請求),但是發生錯誤。

這是我的前端代碼。

公共類MainActivity擴展了Activity {

String url = "http://localhost:8080/test.php";
RequestQueue mRequestQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button send = (Button)findViewById(R.id.button1);
    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
         mRequestQueue = Volley.newRequestQueue(getApplicationContext());
         JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("login.java response", response.toString());

                    int l=  response.length();
                    try {
                        if(l>1){
                            Toast.makeText(getApplicationContext(), response.getString("valid"), Toast.LENGTH_LONG).show();
                        }

                        else{               

                        }   
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }, 

            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("error", "Error: " + error.getMessage());
                    // hide the progress dialog
                    Toast.makeText(getApplicationContext(), "Server Error, try again", Toast.LENGTH_LONG).show();
                }
            });

        // Adding request to request queue
             mRequestQueue.add(jsonObjReq);
        }
    });
}

}

以及后端側碼。

回聲json_encode(“ return”);

它的路徑是:“ c:\\ wamp \\ www \\ test.php”。 如果我運行此程序,它將顯示“服務器錯誤,請重試”。

我可以在網絡瀏覽器上獲得“返回”,因此我認為android程序中的網址存在問題。 我關閉了Windows防火牆,但是它也沒有起作用。

有沒有在這個問題上有豐富經驗並可以幫助我的人?

您應為Android應用提供運行服務器的計算機的實際IP地址。 您可以通過輸入控制台ipconfig (對於Windows), ifconfig對於UNIX系統)來學習IP。 它最有可能是192.168.1.xxx172.16.xxx.xxx

您需要將網址更改為您的IP地址,而不是

"http://localhost:8080/test.php";

使用您的本地IP地址(例如):

"http://192.168.0.1:8080/test.php";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM