我是原生android开发的新手。我使用DB
服务器在phpmyadmin
中创建了xampp
。然后我使用REST webservice
创建了Yii
。我在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
。所以我使用它仍然不起作用。我在warning
中给了logcat
,如下所示
我必须遗漏一些我不知道的事情。我差不多2-3天就坚持了下来,真的不知道该怎么做。
任何帮助都将受到高度赞赏
答案 0 :(得分:0)
打开Android手机/路由器的Wifi热点,将笔记本电脑连接到手机。
在localhost启动服务器(我正在使用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 。
注意:请将您的网络设置为“家庭网络”。设置家庭网络意味着允许您的PC与同一网络上的其他设备共享内容。 如果您使用Windows 10转到WiFi&gt; <当前网络的网络属性>使这台PC可被发现。