2016-07-20 233 views
0

我有这样的代码:HttpAsyncTask不能访问本地主机

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); 
     } 
    } 
} 

我已经尝试通过模拟器这个住址给exceute:“https://10.0.2.2:29422/service/MBBS/TWVpblRlc3RJRGRlc2dlcmFldHMxNTA3MjAxNjE0MTgyMTMyMC41MzQzMjk2NDM0ODM2NDY2/customer

我无法访问,但与同一住址我笔记本电脑: “https://localhost:29422/service/MBBS/TWVpblRlc3RJRGRlc2dlcmFldHMxNTA3MjAxNjE0MTgyMTMyMC41MzQzMjk2NDM0ODM2NDY2/customer”这项工作

任何idee?

+1

为您的计算机提供IP而不是本地主机。它会工作。 –

+0

当我将使用模拟器此localhost地址是:10.0.2.2或? – ange

+0

它是'10.0.2.2',但错误信息是什么? – Joshua

回答

0

由于我的声誉不是+50,我正在写这里。

仿真器工作在的服务器,因此在运行。如果你想访问服务器在移动设备上的本地主机上运行可以访问本地主机 在同一台PC,你必须:

- >如果您的设备和系统在同一个局域网上,你可以将用户系统的IP地址

如果你不知道你的系统使用的IP地址:

ipconfig  //Windows 
ifconfig  //Linux 

OR

- >将静态IP分配给运行服务器的系统

+1

他正在运行模拟器。因此,localhost的ip是'10.0.2.2'。 – Joshua