【发布时间】:2017-06-25 15:52:01
【问题描述】:
我是原生 android 开发的新手。我使用xampp 服务器在phpmyadmin 中创建了一个DB。然后我使用Yii 创建了一个REST webservice。我在ARC 上测试了网络服务,它运行良好。现在我想在我的设备上测试它。所以我搜索了很多文章并找到了答案(link)。但这并没有帮助我。下面是我的代码
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 天,真的不知道该怎么办。
任何帮助将不胜感激
【问题讨论】:
-
您的设备和计算机应该在同一个网络中。使用该网络的 IP 地址并关闭防火墙。
-
好吧,我来告诉你
-
我按照你说的做了我运行了这个应用程序,这个错误显示在
java.net.ConnectException: failed to connect to /192.168.8.85 (port 8000): connect failed: EHOSTUNREACH (No route to host) -
在浏览器中点击 URL 检查它是否正常工作。
-
不,不是,它只适用于
localhost
标签: android android-studio yii xampp