2017-02-08 62 views
1

我是新手到本機android開發。我使用xampp服務器在phpmyadmin中創建了DB。然後我使用Yii創建了一個REST webservice。我測試了ARC上的Web服務,它工作正常。現在我想在我的設備上測試它。所以我搜索了很多文章,並找到了答案(link)。但它並沒有幫助我。下面是我的代碼通過使用Web服務在本地連接android設備

public class MainActivity extends AppCompatActivity { 

String URLGET = "http://192.168.8.85:8000/app/web/users/"; 
String result = ""; 
TextView getData; 
EditText userInput; 
public static final String LOG_TAG = MainActivity.class.getSimpleName(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    getData = (TextView) findViewById(R.id.getData); 
    userInput = (EditText) findViewById(R.id.EnterId); 

    final Button button = (Button) findViewById(R.id.btn_Submit); 


    button.setOnClickListener(new Button.OnClickListener() { 


     @Override 
     public void onClick(View v) { 
      String reqURL = URLGET; 

      new RestOperation().execute(reqURL); 
      //callWebService(query); 
     } 
    }); 

} 

public class RestOperation extends AsyncTask<String, Void, Void> { 


    final HttpClient httpClient = new DefaultHttpClient(); 
    String content; 
    String error; 
    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); 
    String data = ""; 

    // TextView getData = (TextView) findViewById(R.id.getData); 
    // EditText userInput = (EditText) findViewById(R.id.EnterId); 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     progressDialog.setTitle("Please Wait..."); 
     progressDialog.show(); 

     data = "data=" + URLEncoder.encode(String.valueOf(userInput.getText())); 
    } 

    @Override 
    protected Void doInBackground(String... params) { 

     BufferedReader br = null; 

     URL url = null; 
     try { 
      url = new URL(params[0]); 


      URLConnection connection = url.openConnection(); 

      connection.setDoOutput(true); 

      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream()); 

      outputStreamWriter.write(data); 
      outputStreamWriter.flush(); 

      br = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

      StringBuilder sb = new StringBuilder(); 

      String line = null; 

      while ((line = br.readLine()) != null) { 
       sb.append(line); 
       sb.append(System.getProperty("line.separator")); 


      } 

      content = sb.toString(); 

     } catch (MalformedURLException e) { 
      error = " Exception: " + e.getMessage(); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      error = " IOException: " + e.getMessage(); 
      e.printStackTrace(); 
     } finally { 
      try { 
       if(br != null) 
       br.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 

     progressDialog.dismiss(); 

     if (error != null) { 
      getData.setText("Error" + error); 
     } else { 
      getData.setText(content); 


     } 
    } 
}} 

以上ip地址是我的無線地址。首先,我使用localhost在仿真器上進行了測試,但在搜索時發現我應該使用10.0.2.2。所以我使用它,它仍然不起作用。我給我在logcat的一個warning如下圖所示

enter image description hereenter image description here

我必須失去了一些東西,我不知道。我堅持了將近2-3天,真的不知道該怎麼做。

任何幫助將高度讚賞你的Android手機/路由器的WiFi熱點

+0

您的設備和計算機應該在同一個網絡中。使用該網絡的IP地址並關閉防火牆。 –

+0

好吧,讓我這樣做,我會告訴你 – faisal1208

+0

我做了你說我運行應用程序,並顯示此錯誤'java.net.ConnectException:無法連接到/192.168.8.85(端口8000):連接失敗: EHOSTUNREACH(沒有路由到主機)' – faisal1208

回答

0

打開你的筆記本電腦連接到您的手機。

在本地主機啓動服務器(我使用WAMP服務器)

現在,打開命令提示符,然後輸入IPCONFIG 命令你會得到下面的東西

Wireless LAN adapter Wireless Network Connection: 
    Connection-specific DNS Suffix . : 
    Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11 
    IPv4 Address. . . . . . . . . . . : **192.168.43.76** 
    Subnet Mask . . . . . . . . . . . : 255.255.255.0 
    Default Gateway . . . . . . . . . : 192.168.43.1 

拷貝192.168.43.76在您的手機瀏覽器。

注意:請將您的網絡設置爲「家庭網絡」。設置家庭網絡意味着讓您的電腦與同一網絡上的其他設備共享資料。 如果您使用的是Windows 10,請轉到WiFi>當前網絡的網絡屬性>使此PC可被發現。

原創回答:How can I access my localhost from my Android device?

+0

你是什麼意思'打開你的Android手機/路由器的Wifi熱點? 我必須打開'Wifi'或'Wifi Hotspot'這兩個都不同我想 – faisal1208

+0

此答案僅供參考。您可以使用'Android設備的WiFi熱點'或'WiFi'。只是,確保您的設備和計算機處於相同的網絡中,XAMPP/WAPP正在運行,防火牆已關閉。 –

+0

好吧,我可以在網絡瀏覽器中看到結果,但我想訪問我的應用程序,因爲我已發佈屏幕截圖。 – faisal1208