我是新手到本機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
如下圖所示
我必須失去了一些東西,我不知道。我堅持了將近2-3天,真的不知道該怎麼做。
任何幫助將高度讚賞你的Android手機/路由器的WiFi熱點
您的設備和計算機應該在同一個網絡中。使用該網絡的IP地址並關閉防火牆。 –
好吧,讓我這樣做,我會告訴你 – faisal1208
我做了你說我運行應用程序,並顯示此錯誤'java.net.ConnectException:無法連接到/192.168.8.85(端口8000):連接失敗: EHOSTUNREACH(沒有路由到主機)' – faisal1208