我有这段代码:
public class Connexion extends Activity {
EditText etResponse;
TextView tvIsConnected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
// get reference to the views
etResponse = (EditText) findViewById(R.id.etResponse);
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
// check if you are connected or not
if(isConnected()){
tvIsConnected.setBackgroundColor(0xFF00CC00);
tvIsConnected.setText("You are conncted");
}
else{
tvIsConnected.setText("You are NOT conncted");
}
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute("https://10.0.2.2:29422/service/MBBS/TWVpblRlc3RJRGRlc2dlcmFldHMxNTA3MjAxNjE0MTgyMTMyMC41MzQzMjk2NDM0ODM2NDY2/customer");
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
etResponse.setText(result);
}
}
}
我试图通过模拟器对这个问题进行鉴定:&#34; https://10.0.2.2:29422/service/MBBS/TWVpblRlc3RJRGRlc2dlcmFldHMxNTA3MjAxNjE0MTgyMTMyMC41MzQzMjk2NDM0ODM2NDY2/customer&#34;
我无法通过笔记本电脑访问相同的内容: &#34; https://localhost:29422/service/MBBS/TWVpblRlc3RJRGRlc2dlcmFldHMxNTA3MjAxNjE0MTgyMTMyMC41MzQzMjk2NDM0ODM2NDY2/customer&#34;这项工作
任何想法?
答案 0 :(得分:0)
由于我的声望不是+50,我写在这里。
仿真器与服务器运行时在同一台PC上工作,因此可以访问localhost 如果要访问移动设备中Localhost上运行的服务器,则必须:
- &GT;如果您的设备和系统位于同一LAN上,则可以使用系统的IP地址
如果您不知道系统的IP地址,请使用:
ipconfig //Windows
ifconfig //Linux
OR
- &GT;为运行服务器的系统分配静态IP