Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android HTTP通信:_Java_Android_Http - Fatal编程技术网

Java Android HTTP通信:

Java Android HTTP通信:,java,android,http,Java,Android,Http,我创建了一个android应用程序,用于在服务器返回适当答案之前将消息发送到服务器进行解释。我真的在尝试演示无线连接 因此,我只想通过安卓手机发送信息,什么是合适的方式。。。。httppost?(我使用缓冲区/打印对套接字进行了此操作) 在服务器类中,我应该使用httpget来接收消息吗 然后在消化了信息并决定了适当的结果之后,我将如何将其发送回android应用程序?又是httppost 从android应用程序中阅读,我是否需要再次使用httpget 请举个例子。请记住我正在寻找使用http

我创建了一个android应用程序,用于在服务器返回适当答案之前将消息发送到服务器进行解释。我真的在尝试演示无线连接

因此,我只想通过安卓手机发送信息,什么是合适的方式。。。。httppost?(我使用缓冲区/打印对套接字进行了此操作)

在服务器类中,我应该使用httpget来接收消息吗

然后在消化了信息并决定了适当的结果之后,我将如何将其发送回android应用程序?又是httppost

从android应用程序中阅读,我是否需要再次使用httpget

请举个例子。请记住我正在寻找使用http协议

问候


Simon

HTTP POST适用于这两种情况。您可以使用java.net.HttpURLConnection来执行POST


下面是一个很好的例子:通过这个答案链接到:

HTTPPOST在这两种情况下都是合适的。您可以使用java.net.HttpURLConnection来执行POST


下面是一个很好的例子:通过以下答案链接到:

我使用该工具获得了很好的结果

(未运行此操作并省略了try/catch,但它应该可以帮助您开始)

//设置客户端
HttpContext HttpContext=新的BasicHttpContext();
DefaultHttpClient httpClient=新的DefaultHttpClient();
//设置请求
HttpPost=新的HttpPost(“http://someurl.com/");
列表对=新的ArrayList();
添加(新的BasicNameValuePair(“名称”、“值”));
setEntity(新的UrlEncodedFormEntity(对));
//执行请求
基本响应=
(BasicHttpResponse)httpClient.execute(post,httpContext);
//对回应做点什么
InputStream is=response.getEntity().getContent();
BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
字符串内容;
StringBuilder contentBuilder=新的StringBuilder();
字符串行=null;
而((line=br.readLine())!=null)
contentBuilder.append(行);
br.close();
is.close();
content=contentBuilder.toString();
//完成了!

我在使用中取得了很好的效果

(未运行此操作并省略了try/catch,但它应该可以帮助您开始)

//设置客户端
HttpContext HttpContext=新的BasicHttpContext();
DefaultHttpClient httpClient=新的DefaultHttpClient();
//设置请求
HttpPost=新的HttpPost(“http://someurl.com/");
列表对=新的ArrayList();
添加(新的BasicNameValuePair(“名称”、“值”));
setEntity(新的UrlEncodedFormEntity(对));
//执行请求
基本响应=
(BasicHttpResponse)httpClient.execute(post,httpContext);
//对回应做点什么
InputStream is=response.getEntity().getContent();
BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
字符串内容;
StringBuilder contentBuilder=新的StringBuilder();
字符串行=null;
而((line=br.readLine())!=null)
contentBuilder.append(行);
br.close();
is.close();
content=contentBuilder.toString();
//完成了!

此外,您必须在AndroidManifest.xml中设置权限:此外,您必须在AndroidManifest.xml中设置权限:
// setup the client
HttpContext httpContext = new BasicHttpContext();
DefaultHttpClient httpClient = new DefaultHttpClient();

// setup the request
HttpPost post = new HttpPost("http://someurl.com/");
List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
pairs.add(new BasicNameValuePair("name" , "value"));
post.setEntity(new UrlEncodedFormEntity(pairs));

// execute the request
BasicHttpResponse response =
        (BasicHttpResponse)httpClient.execute(post, httpContext);

// do something with the response
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));

String content;
StringBuilder contentBuilder = new StringBuilder();

String line = null;
while((line = br.readLine()) != null)
    contentBuilder.append(line);

br.close();
is.close();

content = contentBuilder.toString();

// done!