我正在使用的代码对我来说工作得很好,但问题是我无法在主要活动中获取该数据
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl = "http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php";
// contacts JSONArray
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("Users");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
String firstname = c.getString("firstname");
String lastname = c.getString("lastname");
String username = c.getString("username");
// show the values in our logcat
Log.e(TAG, "firstname: " + firstname
+ ", lastname: " + lastname
+ ", username: " + username);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {}
}
这是代码new AsyncTaskParseJson()。execute();使这件事发挥作用
但我需要运行
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// we will using AsyncTask during parsing
new AsyncTaskParseJson().execute();
}`
我希望将主要活动中的firstname,lastname,username等数据作为变量。
有可能吗?
当我想获取变量
时,这是我的另一个类IncomingCall.java public class IncomingCall extends BroadcastReceiver {
private String firstname;
private String lastname;
private String username;
public void onReceive (Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, " Calling "+username, Toast.LENGTH_LONG).show();
try {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)
|| intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
notifyuser=true;
}
} catch (Exception e) {
// TODO: handle exception
//Toast.makeText(context, "Error detected 1 "+e, Toast.LENGTH_LONG).show();
}
}
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl = "http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php";
// contacts JSONArray
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("Users");
// loop through all users
// for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(0);
// Storing each json item in variable
firstname = c.getString("firstname");
lastname = c.getString("lastname");
username = c.getString("username");
// show the values in our logcat
Log.e(TAG, "firstname: " + firstname
+ ", lastname: " + lastname
+ ", username: " + username);
// }
} catch (JSONException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//Here you use your variables
}
});
return null;
}
protected void onPostExecute(String strFromDoInBg) {
Log.e("TAG1", "firstname: " + firstname
+ ", lastname: " + lastname
+ ", username: " + username);
}
}
这是我的代码
答案 0 :(得分:1)
将您的代码放在Main Activity类中,然后使用类变量来存储您想要的内容,例如:
public class MainActivity extends Activity {
private String[] firstname;
private String[] lastname;
private String[] username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AsyncTaskParseJson().execute();
}
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl = "http://demo.codeofaninja.com/tutorials/json-example-with-php/index.php";
// contacts JSONArray
JSONArray dataJsonArr = null;
@Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... arg0) {
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("Users");
firstname = new String[dataJsonArr.length()];
lastname = new String[dataJsonArr.length()];
username = new String[dataJsonArr.length()];
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
firstname[i] = c.getString("firstname");
lastname[i] = c.getString("lastname");
username[i] = c.getString("username");
// show the values in our logcat
Log.e(TAG, "firstname: " + firstname
+ ", lastname: " + lastname
+ ", username: " + username);
}
} catch (JSONException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//Here you use your variables
}
});
return null;
}
@Override
protected void onPostExecute(String strFromDoInBg) {}
}
}
这样的事情(它没有错误检查,试一试)
编辑:请务必在android清单中声明互联网权限:
<uses-permission android:name="android.permission.INTERNET" />
答案 1 :(得分:1)
onPostExecute方法在主线程上运行,doInBackground完成后需要使用数据并控制返回主线程。
最好在方法中使用这些数据
protected void onPostExecute(String strFromDoInBg) {
// use the firstname , lastname or username after this method call.
}