多亏了wamp服务器,我试图将我的android应用程序连接到本地主机url,但它无法正常工作。我的目标是获取json数据并解析这些数据。对于我的测试,我使用的是设备而不是模拟器,并且我在AndroidManifest.xml中使用权限:
<uses-permission android:name="android.permission.INTERNET" />
我的网址看起来像这样:
String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
我尝试过:
http://localhost/
http://10.0.2.2:8080/
http://10.0.2.2/
但是到目前为止,它从未成功:
java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)
然后我尝试了在互联网上找到的json url测试:http://headers.jsontest.com/
它的工作真的很好,我在这个地址得到json数据。所以我想我的代码很好,这里的问题是我的本地主机url,我不知道它的确切形式是什么。我阅读了许多有关此主题的主题,但没有找到解决方案。
这里是我的代码:
主要活动:
public class MainActivity extends Activity {
private String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
private ListView lv = null;
private Button bGetData;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final JsonDownloaderTask task = new JsonDownloaderTask(this);
lv = (ListView) findViewById(R.id.list);
bGetData = (Button)findViewById(R.id.getdata);
bGetData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
task.execute(url);
}
});
}
public void jsonTaskComplete(JSONArray data){
//todo
}
}
AsyncTask:
public class JsonDownloaderTask extends AsyncTask<String, String, JSONArray> {
MainActivity ma;
public JsonDownloaderTask(MainActivity main){
ma = main;
}
@Override
protected JSONArray doInBackground(String... url) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONArray jsonArray = null;
try {
jsonArray = jParser.getJSONFromUrl(url[0]);
} catch (IOException e) {
e.printStackTrace();
}
return jsonArray;
}
protected void onPostExecute(JSONArray data){
ma.jsonTaskComplete(data);
}
}
JSONParser:
public class JSONParser {
String data = "";
JSONArray jsonArray = null;
InputStream is = null;
public JSONParser(){}
// Method to download json data from url
public JSONArray getJSONFromUrl(String strUrl) throws IOException{
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
is = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
is.close();
data = sb.toString();
//br.close();
jsonArray = new JSONArray(data);
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
is.close();
}
return jsonArray;
}
}
首先,您必须在eclipse设置中绑定运行服务器的计算机的IP地址。
您可以这样操作。
“>
右键单击Eclipse中的PHP
项目,然后运行配置,然后在Web Application
中找到Argument
选项卡。现在,在此处提供运行服务器的计算机的端口和LAN IP地址。
类似这样的东西[[--port = 8888 --address = 192.168.1.6
,然后将URL更新为http://192.168.1.6:8080/tests/PhpProject1/connectionBDD.php
在我的情况下,这是我的LAN IP地址192.168.1.6,在那您必须使用ipconfig
,ifconfig
之类的网络命令找到它,并使用该IP地址。“ 10.0.2.2:80/mysitename/page.php”
[Keyvey]],它将根据您的IP地址自动生成一个url。这是链接:
我使用仿真器以及在设备中测试了该应用程序。两种情况都可以解决。