我是Android的新手,我嘗試使用php腳本與訪問簡單數據庫的本地主機通信。 我在doInBackground()方法中定義了一個任務,該方法從本地主機上存儲的數據庫獲取值(我不知道該部分是否可以工作)。 我想使用doInBackground方法返回的結果在活動的textview中設置文本。使用doInBackground的結果來設置textView
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
BackgroundWorker(Context ctx)
{
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String group = params[0];
String child = params[1];
String address = "http://10.0.2.2/conn.php";
URL url = null;
try {
url = new URL(address);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("group", "UTF-8") + "=" + URLEncoder.encode(group, "UTF-8") + "&"
+ URLEncoder.encode("child", "UTF-8") + "=" + URLEncoder.encode(child, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line;
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
以及活動類:
public class viewTT extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_tt);
Button btnNextScreen = (Button) findViewById(R.id.button);
TextView txtName = (TextView) findViewById(R.id.textView);
TextView txtName2 = (TextView) findViewById(R.id.textView2);
Intent i = getIntent();
// Receiving the Data
String group= i.getStringExtra("group");
String child = i.getStringExtra("child");
txtName.setText(group+" "+child);
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(group,child);
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), MainActivity.class);
startActivity(nextScreen);
}
});
}
}
我想設置txtName2。
後'doInBackground'稱爲返回的結果,然後將這些結果傳遞給'onPostExecute'在這裏你可以設置'txtName2.setText(結果);' –
是'BackgroundWorker'和' viewTT'物理2個不同的文件? – Yazan
[如何從另一個類更新活動的TextView]的可能重複(http://stackoverflow.com/questions/10996479/how-to-update-a-textview-of-an-activity-from-another-類) – Yazan