我對Android有點新,並且一直在用XML字符串的HTTP Post進行嘗試。我已經嘗試了使用HttpsUrlConnection和HttpPost的這段代碼的5個版本,以及Im運行的麻煩是我的XML字符串沒有將它傳遞給我的服務器上的應用程序,而是請求和XML將它傳遞給Apache服務器。 我試圖完成的是將XML中的用戶名和PIN發送到我的服務器上的apache perl cgi XML。我已經使用GET完成了它,並且工作得很好,但似乎無法讓POST工作。 任何我可能會做錯的見解,特別是如果我的代碼看起來可以完成我的目標,將不勝感激。再次道歉如果這是一個非常新手的問題。 謝謝你們:)將HTTP字符串發佈到HTTP的問題Android Android HttpPost和HttpUrlConnection
來自主要活動的XML字符串是一個普通的XML,我添加xmlsrc =之前發送到該函數。
String xml = "<?xml version=\"1.0\"encoding=\"UTF-8\"?><UserRequesting><NewUser>joseph</NewUser><Password>123456789</Password></UserRequesting>";
這第一個片段是我最新的使用HttpsURLConnection的:
public void fetchLoginXML(){
Log.d(TAG, "IN fetch ");
HttpsURLConnection urlc;
OutputStreamWriter out = null;
DataOutputStream dataout;
BufferedReader in = null;
try {
URL url = new URL(urlValuser);
Log.d(TAG, "Final URL: " + url);
urlc = (HttpsURLConnection) url.openConnection();
urlc.setHostnameVerifier(new AllowAllHostnameVerifier());
urlc.setRequestMethod("POST");
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-Type", "text/xml");
// perform POST operation
Log.d(TAG, "Xml Source to POST: " + xmlsrc);
String body = xmlsrc;
OutputStream output = new BufferedOutputStream(urlc.getOutputStream());
output.write(body.getBytes());
output.flush();
int responseCode = urlc.getResponseCode();
in = new BufferedReader(new InputStreamReader(urlc.getInputStream()),8096);
String response = "";
String line = in.readLine();
while (line != null) {
response += line;
line = in.readLine();
}
Log.d(TAG, "Post results Response Code " + responseCode);
Log.d(TAG, "Post results Response " + response);
in.close();
factory = XmlPullParserFactory.newInstance();
XmlPullParser myparser = factory.newPullParser();
Log.d(TAG, "Setting myparser paramaters ");
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
Log.d(TAG, "Setting myparser input into xmldata ");
myparser.setInput(new StringReader(response));
Log.d(TAG, "send myparser to function parsexmlandstoreit ");
parseXMLAndStoreIt(myparser);
} catch (Exception e) {
Log.e(TAG, "Error Posting Data: " + e.toString());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我的第二個版本使用HttpPost:
public void postData(String sendData) throws Exception {
// Create a new HttpClient and Post Header
Log.d(TAG, "Sending Data: " + sendData);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://joes....");
httppost.addHeader("Accept", "text/xml");
try {
StringEntity se = new StringEntity(sendData);
se.setContentType("text/xml");
httppost.setEntity(se);
// Execute HTTP Post Request
Log.d(TAG, "Execute HTTP POST");
HttpResponse response = httpclient.execute(httppost);
Log.d(TAG, "Message Sent :)");
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
if(response.getStatusLine().getStatusCode()!= HttpStatus.SC_OK)
{
Log.e(TAG, "Response: " + response.getStatusLine().getReasonPhrase());
throw new Exception(response.getStatusLine().getReasonPhrase());
}
Log.d(TAG, "Response: " + response.getStatusLine().getReasonPhrase());
String received = "";
String line = buf.readLine();
while (line != null) {
received += line;
line = buf.readLine();
}
StringBuilder sb = new StringBuilder();
String s;
while(true)
{
s = buf.readLine();
if(s==null || s.length()==0)
break;
sb.append(s);
}
buf.close();
ips.close();
sb.toString();
factory = XmlPullParserFactory.newInstance();
XmlPullParser myparser = factory.newPullParser();
Log.d(TAG, "Setting myparser paramaters ");
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
Log.d(TAG, "Setting myparser input into xmldata ");
myparser.setInput(new StringReader(received));
Log.d(TAG, "send myparser to function parsexmlandstoreit ");
parseXMLAndStoreIt(myparser);
}
catch (ClientProtocolException e)
{
Log.d(TAG, "Client Protocol Error: " + e);
e.printStackTrace();
// TODO Auto-generated catch block
}
catch (IOException e)
{
Log.d(TAG, "I/O Error: " + e);
e.printStackTrace();
// TODO Auto-generated catch block
}
}
請解釋背後的Apache服務器。哪一種? – greenapps
它是linux/apache 2.2 – fluxingjoe
廢話。這不是我的問題的答案。你在談論'一個網絡服務器應用程序'嗎? – greenapps