[英]How can I send http messages from android app( physical device) to my local server using mobile hotspot
what i am doing. 我在做什么。
I start xampp server. 我启动xampp服务器。
I make connection mobile(Micromax 117) hotspot with laptop 用笔记本电脑使连接移动(Micromax 117)热点
In my device there is app from that i want to send request to local server. 在我的设备中,有一个我想向本地服务器发送请求的应用程序。 I am using local IP address(IPv4) which is display on cmd in url.
我正在使用本地IP地址(IPv4),该地址显示在url中的cmd上。
Like :- http://192.168.50.32:9090/ 像: -http : //192.168.50.32 : 9090/
Here 9090 is my port number 9090是我的端口号
code: - 代码:-
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL("http://192.168.43.18:9090/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(false);
connection.setRequestProperty("Content-Type", "text/html");
connection.setRequestProperty("Accept", "text/html");
InputStream stream = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String br = bufferedReader.readLine();
do {
Log.e("Data is", br);
// Toast.makeText(MainActivity.this,br,Toast.LENGTH_LONG).show();
} while (br != null);
} catch (Exception e) {
Log.e("TAG", "doInBackground:" + e.toString());
}
return null;
}
I just want to know connection is working or not But nothing Working. 我只想知道连接是否正常工作,但没有任何工作。
Thanks . 谢谢 。
If you are running in local host you need to use 10.0.2.2 or if your computer is connected to network, you need to make the server listen to your network ip address. 如果您在本地主机上运行,则需要使用10.0.2.2,或者如果您的计算机已连接到网络,则需要使服务器侦听网络IP地址。
try this method once 一次尝试这种方法
public String performPostCall(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}
else {
response="";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.