![](/img/trans.png)
[英]How can I send http messages from android app( physical device) to my local server without using internet connection?
[英]How can I send http messages from android app( physical device) to my local server using mobile hotspot
我在做什么。
我启动xampp服务器。
用笔记本电脑使连接移动(Micromax 117)热点
在我的设备中,有一个我想向本地服务器发送请求的应用程序。 我正在使用本地IP地址(IPv4),该地址显示在url中的cmd上。
像: -http : //192.168.50.32 : 9090/
9090是我的端口号
代码:-
@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;
}
我只想知道连接是否正常工作,但没有任何工作。
谢谢 。
如果您在本地主机上运行,则需要使用10.0.2.2,或者如果您的计算机已连接到网络,则需要使服务器侦听网络IP地址。
一次尝试这种方法
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.