我想連接我的android應用程序到本地主機的網址,這要歸功於wamp服務器,但它不起作用。我的目標是獲取json數據並解析這些數據。對於我的測試,我使用的是設備而不是模擬器和我使用的權限在AndroidManifest.xml:Android連接到本地主機
<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;
}
}
感謝您的回答,我現在用NetBeans管理這個,urlConnection.connet();工作正常,但我有另一個異常:java.net.SocketException:recvfrom失敗:ECONNRESET(連接重置由同行),也許你知道這個問題 – AlexDG
嗯,對於這個新問題,你可以在這裏閱讀http://stackoverflow.com/questions/11207394/getting-socketexception-connection-reset-by-peer-in-android現在你可以接受答案 –
謝謝你的幫助。 – AlexDG