« 回覆文章 #6 於: 一月 13, 2015, 04:57:13 pm »
(6) 修改 QueryActivity.java
public void submitQueryActivity(View view) {
// Do something in response to button
TextView tv = (TextView) findViewById(R.id.textView4);
tv.setText("查詢中...");
new NetActionTask().execute("http://clinic.aiplab.net/app_query_reg.php");
}
public void showRegisterActivity(View view) {
// Do something in response to button
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}
public void showOrderActivity(View view) {
// Do something in response to button
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
private class NetActionTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
return postToNetwork(urls[0]);
} catch (IOException e) {
return getString(R.string.connection_error);
}
}
/**
* Uses the logging framework to display the output of the fetch
* operation in the log fragment.
*/
@Override
protected void onPostExecute(String result) {
//這裡要換頁
TextView tv = (TextView) findViewById(R.id.textView4);
tv.setText(result);
}
}
/** Initiates the fetch operation. */
private String postToNetwork(String urlString) throws IOException {
InputStream stream = null;
String str ="NONO";
try {
stream = fromUrl(urlString);
str = readIt(stream, 500);
} finally {
if (stream != null) {
stream.close();
}
}
return str;
}
/**
* Given a string representation of a URL, sets up a connection and gets
* an input stream.
* @param urlString A string representation of a URL.
* @return An InputStream retrieved from a successful HttpURLConnection.
* @throws java.io.IOException
*/
private InputStream fromUrl(String urlString) throws IOException {
// BEGIN_INCLUDE(get_inputstream)
URL url = new URL(urlString);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
//Get parameters
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
String paramName = editText1.getText().toString().trim(); //e.g. "James Wang";
String paramID = editText2.getText().toString().trim(); //e.g."K111222333";
// Start the query
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("PatientID", paramName));
params.add(new BasicNameValuePair("PatientName", paramID));
OutputStream os = http.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
http.connect();
InputStream stream = http.getInputStream();
return stream;
// END_INCLUDE(get_inputstream)
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
/** Reads an InputStream and converts it to a String.
* @param stream InputStream containing HTML from targeted site.
* @param len Length of string that this method returns.
* @return String concatenated according to len parameter.
* @throws java.io.IOException
* @throws java.io.UnsupportedEncodingException
*/
private String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}