我想通过Android调用web服务.我需要通过HTTP将一些XML发布到URL.我发现这是剪辑发送POST,但我不知道如何包含/添加XML数据本身.
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.10.4.35:53011/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Content-Type", "application/soap+xml"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Where/how to add the XML data?
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Run Code Online (Sandbox Code Playgroud)
这是我需要模仿的完整POST消息:
POST /a8103e90-f1e3-11dd-bfdb-8b1fcff1a110 …Run Code Online (Sandbox Code Playgroud) 我正在为一个Android项目做一个Get和Post方法,我需要将HttpClient 3.x"翻译"到HttpClient 4.x(由android使用).我的问题是,我不确定我做了什么,我找不到某些方法的"翻译"......
这是我做过的HttpClient 3.x和( - >)HttpClient 4.x"翻译"如果我找到它(只有那些问我问题的人):
HttpState state = new HttpState (); --> ?
HttpMethod method = null; --> HttpUriRequest httpUri = null;
method.abort(); --> httpUri.abort(); //httpUri is a HttpUriRequest
method.releaseConnection(); --> conn.disconnect(); //conn is a HttpURLConnection
state.clearCookies(); --> cookieStore.clear(); //cookieStore is a BasicCookieStore
HttpClient client = new HttpClient(); --> DefaultHttpClient client = new DefaultHttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(SOCKET_TIMEOUT) --> HttpConnectionParams.setConnectionTimeout(param, SOCKET_TIMEOUT);
client.setState(state); --> ?
client.getParams().setCookiePolicy(CookiePolicy.RFC_2109); --> HttpClientParams.setCookiePolicy(param, CookiePolicy.RFC_2109);
PostMethod post = (PostMethod) method; --> ?
post.setRequestHeader(...,...); --> conn.setRequestProperty(...,...);
post.setFollowRedirects(false); --> …Run Code Online (Sandbox Code Playgroud) 我是Android应用程序开发的新手,我需要的是我有两个文本框用户名和密码,它会发布到服务器并使用php页面检查它,如果登录成功则转到下一个屏幕,否则显示一个msg框显示登录错误我该怎么办?
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://google.com");
EditText tw =(EditText) findViewById(R.id.EditText01);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
tw.setText(status);
} catch (ClientProtocolException e) {
tw.setText(e.toString());
} catch (IOException e) {
tw.setText(e.toString());
}
}
Run Code Online (Sandbox Code Playgroud)