我想使用我的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服务?
答案 0 :(得分:1)
如果要从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
}
答案 1 :(得分:1)
您的应用无法“点击”提交按钮。你说提交按钮提交了所有表格。您的应用应该执行提交按钮触发的请求。
答案 2 :(得分:1)
我必须更改Web服务并使其获得GET和POST,然后我必须将我的代码放入
conn.setRequestMethod("GET");