[英]android request a php web service using HttpURLConnection and submit the php form
我想用我的android應用程序解析一個php web服務,我創建了一個這樣的url連接
InputStream stream = null ;
URL url = new URL(url);
HttpURLConnection conn;
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setReadTimeout(30000);
stream =conn.getInputStream();
在getInputStream
之后,我使用pull解析器來解析xml數據,但我的問題是Web服務需要提交表單來獲取xml數據。 如何從我的java代碼中單擊此按鈕? 這是可能的還是我需要更改Web服務?
如果要從webservide讀取數據,請執行以下步驟:在按鈕的onclick監聽器中
new urlConTask().execute() //use asynctask
在asynctask的doInBackground方法中:
@Override
protected String doInBackground(String... params) {
String url = ur url here;
String response = "";
try {
response = HttpConnect.sendGet(url);//sendGet is method defined in HttpConnect.java. Its a custom class
} catch (Exception e) {
e.printStackTrace();
}
sendGetMethod:
public static String sendGet(String url) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
return response.toString(); //this is your response
}
您的應用無法“點擊”提交按鈕。 你說提交按鈕提交了所有表格。 您的應用應該執行提交按鈕觸發的請求。
我必須更改Web服務並使其獲得GET和POST,然后我必須放入我的代碼
conn.setRequestMethod("GET");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.