2016-07-20 2 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"この作品

anye?

+1

ローカルホストではなくマシンIPを指定します。それが動作します。 –

+0

私はエミュレータでこのlocalhostアドレスを使って作業します:10.0.2.2または? – ange

+0

これは '10.0.2.2'ですが、エラーメッセージは何ですか? – Joshua

答えて

0

私の評判は+50ではないので、私はここに書いています。

エミュレータは、サーバーは、あなたのモバイルデバイスにローカルホスト上で実行しているサーバーにアクセスする場合はlocalhost にアクセスすることができますので、実行されているのと同じPC上で動作

は、あなたが持っているだろうに。

- >もしあなたのデバイスとシステム

ipconfig  //Windows 
ifconfig  //Linux 

OR

01:あなたのシステム使用のIPアドレスがわからない場合は、ユーザシステム

のIPアドレスをすることができ、同じLAN上にあります

- >サーバーを実行しているシステムに静的IPを割り当てます。

+1

彼はエミュレータを実行しています。したがって、localhostのipは '10.0.2.2'です。 – Joshua

関連する問題