0
我想異步類的結果數據添加到字符串變量,我傳遞一個字符串變量從主類,而我創建異步類的OBJ並調用它的過載構造器,並在POST方法的異步類我分配結果值的字符串,但是當我在主類中打印該字符串,我沒有得到結果值(後方法結果值),而當我執行同樣的過程,而傳遞文本而不是字符串從主類和設置結果值到該textview,然後從主得到的價值我有結果值,但我想通過傳遞字符串不textview相同的功能。的Android異步類崗位功能
這就是我對你的呼喚asyncclass:
getCountryId getCountryid=new getCountryId(this, roleField); // rolefield is textview
getCountryid.execute(itemcountry);
這是我的異步類:
public class getCountryId extends AsyncTask<String,Void,String> {
private Context context;
private TextView role;
public getCountryId(Context context, TextView role){
this.context=context;
this.role=role;
}
protected void onPreExecute(){
}
@Override
protected String doInBackground(String... arg0) {
try{
String z = (String)arg0[0];
String link="http://ipadress/regform/getstateid.php";
String data = URLEncoder.encode("z", "UTF-8") + "=" + URLEncoder.encode(z, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
role.setText(result);
}
}
我在我的post方法和我的主類中設置了異步的結果值到Textview(角色),我通過調用該TextView的getText()函數來獲取文本。 –
感謝您的幫助。 –