我目前正在编写一个Android应用程序,它将特定文本上传到Web服务器进行数据收集,对于php而言,我是一个完整的菜鸟,我想要一个如何解析服务器上给定数据的示例将其保存到服务器上的文件中。
try {
// write to the stream
StringBuilder data = new StringBuilder();
String numData = URLEncoder.encode("phoneId", "UTF-8") + "="
+ URLEncoder.encode(mynumber, "UTF-8");
String emailData = URLEncoder.encode("primaryEmail", "UTF-8") + "="
+ URLEncoder.encode(primaryEmail, "UTF-8");
String passData = URLEncoder.encode("phoneModel", "UTF-8") + "="
+ URLEncoder.encode(phoneModel, "UTF-8");
data.append(numData).append("&").append(emailData).append("&").append(phoneData);
wr = new OutputStreamWriter(connect.getOutputStream());
wr.write(data.toString());
wr.flush();
// read the result from the server
rd = new BufferedReader(new InputStreamReader(connect.getInputStream()));
sb = new StringBuilder();
String line = null;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
Log.e("URL INVALID:", "The url given, " + urlString + ", is invalid.");
return;
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
// close the connection, set all objects to null
connect.disconnect();
rd = null;
sb = null;
wr = null;
connect = null;
}