我正在尝试在异步任务之外使用 json 对象,但我的班级无法识别它。
如何将 JSONObject 传递到 Async 任务的外部?
我唯一能找到的是试图用来onProgressUpdate()
传递对象,但我尝试实现并且对象会出现空白:(。
任何帮助将不胜感激,谢谢。
这是我的课:
public class NewHomepage extends Activity {
public static String url = "http://www.alkouri.com/android/SQL.php?username=";
public static String usernamefromlogin;
public static TextView errorchecking;
public static JSONArray user = null;
//JSON Node Names
public String TAG_USER = "users";
public String TAG_FIRST = "first";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reshomepage);
//get data from previous screen
Intent intent = getIntent();
getIntent().getExtras();
//convert intent (intent) to string called "usernamefromlogin"
//error checking in log cat to see value of "usernamefromlogin"
usernamefromlogin = intent.getExtras().getString("username2");
Log.d("log usernamefromlogin", usernamefromlogin);
//take the string "url" and add string "usernamefromlogin" after it
//error checking in log cat to see value of url5
String url5 = url.concat(usernamefromlogin);
Log.d("log url5", url5);
//start asynch task
class PostTask2 extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}//end PreExecute
@Override
protected String doInBackground(String... params) {
//pass url from outside class to inside this class
String url5 = params[0];
//Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url5);
return null;
}//end doInBackground
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}//end onProgressUpdate
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}//end onPostExecute
}//end Async task
//execute the Async task
new PostTask2().execute(url5);
}//end oncreate
@Override
public void onBackPressed() {
// do nothing on back press
}
}//end class