我有一个字符串,它已经在正确的URLEn codeD表格格式,并希望通过它在Android POST请求到PHP的服务器发送。我知道这个方法发送URL连接codeD的形式在Android上使用的UrlEn$c$cdFormEntity我知道如何使用它。这问题是数据进入功能已经URL连接codeD和&符号连接,因此使用 UrlEn codedFormEntity
将涉及很多额外的努力把它变成一个列表
namevaluepairs中
的,我宁愿不要。
那么,如何作出正确的POST请求发送这样的字符串作为内容主体?
我已经尝试过使用StringEntity,但PHP服务器没有得到任何数据(空 $ _ POST
)对象。
我正在测试针对 http://test.lifewanted.com/echo.json.php 它简单地为
<?PHP的回声json_en code($ _ REQUEST);
下面是一个例子已经-CN codeD数据:
partnerUserID =电子邮件%40example.com和放大器; partnerUserSecret =输入mypassword和放大器;命令=验证
解决方案如果你不介意使用的的HttpURLConnection
代替(推荐) HttpClient的
,那么你可以这样来做:
公共无效performPost(字符串连接codedData){
HttpURLConnection的urlc = NULL;
OutputStreamWriter了= NULL;
DataOutputStream类DATAOUT = NULL;
BufferedReader中的= NULL;
尝试 {
网址URL =新的URL(URL_LOGIN_SUBMIT);
urlc =(HttpURLConnection类)url.openConnection();
urlc.setRequestMethod(POST);
urlc.setDoOutput(真正的);
urlc.setDoInput(真正的);
urlc.setUseCaches(假);
urlc.setAllowUserInteraction(假);
urlc.setRequestProperty(HEADER_USER_AGENT,HEADER_USER_AGENT_VALUE);
urlc.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
DATAOUT =新DataOutputStream类(urlc.getOutputStream());
//执行POST操作
dataout.writeBytes(EN codedData);
INT响应code = urlc.getResponse code();
在=新的BufferedReader(新的InputStreamReader(urlc.getInputStream()),8096);
字符串响应;
//写HTML到System.out调试
而((响应= in.readLine())!= NULL){
的System.out.println(响应);
}
附寄();
}赶上(的ProtocolException E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
} 最后 {
如果(满分!= NULL){
尝试 {
out.close();
}赶上(IOException异常E){
e.printStackTrace();
}
}
如果(在!= NULL){
尝试 {
附寄();
}赶上(IOException异常E){
e.printStackTrace();
}
}
}
}
I have a string that is already in the proper URLEncoded Form format and would like to send it through a POST request on Android to a PHP server. I know the method for sending URL encoded forms on Android uses the UrlEncodedFormEntity
and I know how to use it. The problem with that is that the data comes into the function already URL encoded and joined by ampersands, so using UrlEncodedFormEntity
would involve a lot of extra work to turn it into a List
of NameValuePairs
and I'd rather not.
So, how do I make a proper POST request sending this string as the content body?
I have already tried using StringEntity
, but the PHP server didn't get any of the data (empty $_POST
object).
I am testing against http://test.lifewanted.com/echo.json.php which simply is
<?php echo json_encode( $_REQUEST );
Here is an example of the already-encoded data:
partnerUserID=email%40example.com&partnerUserSecret=mypassword&command=Authenticate
解决方案
If you don't mind using an HttpURLConnection
instead of the (recommended) HttpClient
then you could do it this way:
public void performPost(String encodedData) {
HttpURLConnection urlc = null;
OutputStreamWriter out = null;
DataOutputStream dataout = null;
BufferedReader in = null;
try {
URL url = new URL(URL_LOGIN_SUBMIT);
urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestMethod("POST");
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty(HEADER_USER_AGENT, HEADER_USER_AGENT_VALUE);
urlc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
dataout = new DataOutputStream(urlc.getOutputStream());
// perform POST operation
dataout.writeBytes(encodedData);
int responseCode = urlc.getResponseCode();
in = new BufferedReader(new InputStreamReader(urlc.getInputStream()),8096);
String response;
// write html to System.out for debug
while ((response = in.readLine()) != null) {
System.out.println(response);
}
in.close();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
上一篇:如何使用Android 4.4系统的打印框架打印PDF如何使用、框架、系统、Android
下一篇:Android的 - 如何启动谷歌地图意图在某些位置,缩放级别和标记Android应用程序缩放、意图、应用程序、标记