当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Connecting android to a local server using XAMPP

问题描述:

I'm trying to fetch data from my local server to an android device, I was using a free online hosting service where everything worked fine before I encountered some non-related technical issues, so it's not likely the problem lies in the code itself(just a thought). Anyway, when I migrated to a local server using XAMPP, I get no response on my app of what so ever, I accessed the php file from the mobile's browser and it's giving the right results, but the app itself gives complete silence. Searching through SO, most questions mentioned putting the right IP address in the file's URL. I'm using volley and I've inserted the very same URL with with I accessed the file in the browser, the server doesn't seem to respond at all.

Here's the php code I'm using:

<?php

include 'dbConnect.php';

$searchTerm='d';

$query = $conn->prepare('SELECT term,id FROM tag WHERE term LIKE :searchTerm');

$query->execute(array(':searchTerm'=>$searchTerm.'%'));

$result=$query->fetchAll(PDO::FETCH_ASSOC);

echo json_encode(array('result'=>$result));

?>

I'm using Volley library to contact the server, the first file is

CustomeAutoCompleteTextViewTextChangedListener.java

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

try {

NetworkRequsts networkRequsts = new NetworkRequsts(context);

networkRequsts.tagsJsonObjectRequest("https://192.168.110.1:8080/Register.php");

}

catch (Exception e)

{

e.printStackTrace();

}

}

This is where the request should be issued, notice the URL, this is where I think the problem is since it's the only thing I've changed in the code when I changed the server.

Here's the Volley request:

NetworkRequsts.java

JsonObjectRequest tagJsonRequest=new JsonObjectRequest(url,tagSearchObject, new Response.Listener<JSONObject>() {

@Override

public void onResponse(JSONObject response) {

try {

Log.d("Search term object",tagSearchObject.toString());

Log.d("respone Test", response.length()+" "+response.toString());

JASONArrayResponse=response.getJSONArray("result");

for(int i=0;i<JASONArrayResponse.length();i++)

{

JSONObject jsonObject=JASONArrayResponse.getJSONObject(i);

Log.d("Volley", jsonObject.getString("term"));

Log.d("searchTerm",register.tagsAutoCompleteTextView.getText().toString());

}

}

catch(JSONException JE)

{

JE.printStackTrace();

}

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

if (error instanceof ServerError) {

Log.e("Volley", "ServerError");

Toast.makeText(context.getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();

}

}

});

网友答案:

First of all check whether the server is working perfectly, load in the browser using localhost and if works without fail, then change the ip address to the system ip. Then check your network connection properties where sharing is limited. Most of the cases the connection failure occurs because of the sharing limited by the Anti Virus program or something like that. After giving all the permissions it will work

分享给朋友:
您可能感兴趣的文章:
随机阅读: