我想发送一个字符串" hello world"通过流到服务器。
到目前为止我做了:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ru.my.project</groupId>
<artifactId>my-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>my-module-1</artifactId>
<packaging>jar</packaging>
<name>This is one of my modules</name>
<dependencies>
<dependency>
<groupId>ru.my.project</groupId>
<artifactId>my-module-2</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--I want to import dependencies for test from this pom:-->
<dependency>
<groupId>ru.my.project.test</groupId>
<artifactId>my-module-1-test</artifactId>
<type>pom</type>
<version>1.0</version>
<scope>import</scope>
</dependency>
</dependencies>
</project>
但不确定我在哪里放置我要发送数据的服务器的IP和端口?
我把我想要发送的字符串放在哪里&#34; hello world&#34; ?
我在这种方法上遇到错误:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ru.my.project.test</groupId>
<artifactId>my-module-1-test</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Dependencies for testing module-1</name>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.11.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.11.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
在网址上:无法解析符号网址
private void PostData() {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
writeStream(out);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
}
}
在urlConnection.getOutputStream()未处理的异常。
开启
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
无法解析方法writeStream
readStream相同
最后终于:最后没有尝试
我从官方开发者android com:
中取了示例答案 0 :(得分:2)
您可以执行以下操作: -
from selenium.webdriver.chrome.webdriver import WebDriver
import io
csvdoc = "your_path/file.csv"
driver = WebDriver("your_path/chromedriver.exe")
driver.get("http://google.com")
element = driver.find_element_by_id("lst-ib")
with io.open(csvdoc, 'r') as csvfile:
csvcontent = csvfile.read()
print(csvcontent)
for l in csvcontent.splitlines():
line = l.split(',')
element.send_keys(line[0])
element.send_keys(line[1])
element.send_keys(line[2])
答案 1 :(得分:2)
你是全新的,因为我可以看到,并且android链接在示例中有一些错误。对于每个&#34;网络&#34;为你的任务工作,你必须为android异步操作,你还必须为你的清单文件添加网络权限。
例如:
注意:writeStream
和readStream
是您希望他们完成的工作的自定义函数。
private void PostData() {
String IPPORT = "www.android.com"; // or example 192.168.1.5:80
URL url = new URL("http://"+IPPORT+"/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
writeStream(out);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
}
finally {
urlConnection.disconnect();
}
}
writeStream函数示例
private void writeStream(OutputStream out){
String output = "Hello world";
out.write(output.getBytes());
out.flush();
}
答案 2 :(得分:0)
好吧,我担心你必须定义变量“url”和方法“writeStream”和“readStream”。
“url”应该是一个网址对象,其中包含您要连接的地址(您在浏览器中输入的内容),请参阅here。
WriteStream是您将数据写入服务器的地方,也就是您编写“Hello World”的地方。
ReadStream将读取服务器的响应。遗憾的是,在这样的帖子中,阅读和提交数据并不容易解释,但有足够的教程可以帮助您。
答案 3 :(得分:0)
您应该阅读Java中的流,并记住您必须将网络操作放在Android中的单独线程中。
答案 4 :(得分:-1)
I had the same problem but with this code, I can solve
String userAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0";
String address = "http://localhost/form.php";
String forSending = "cadena";
String charset = "UTF-8";
String stringToSend = URLEncoder.encode(forSending, charset);
URL URL = new URL(address);
URLConnection connection = URL.openConnection();
connection.addRequestProperty("User-Agent", userAgent);
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write("nombre=" + stringToSend);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String response;
while((response = in.readLine()) != null)
System.out.println(response);
in.close();
帮助链接:http://aljavier.github.io/manipulando-urls-y-haciendo-solicitudes-http-con-java.html Derechos de autor