我是Android的初学者,我想使用我的数据库将名称打印到滑动视图中。我从Android开发人员网站(http://developer.android.com/training/implementing-navigation/lateral.html)下载了示例项目。我试图使用AsyncTask更改代码来调用数据库,但我没有成功。你知道怎么做吗? 我遇到的主要问题是makehttprequest在我的片段中不起作用(从asynctask进入doinbackground)
public class DemoObjectFragment extends Fragment {
public static final String ARG_OBJECT = "object";
private static final String url ="http://www.golinkr.net";
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String TAG_SUCCESS = "success";
private static final String TAG_PROFILE_INFO = "Profile_Info";
private static final String TAG_FIRST_NAME = "First_Name";
private static final String SELECT_FUNCTION = "getProfile";
private static final String ID = "2";
JSONArray profileInfos = null;
HashMap<String,String> profile;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
profile = new HashMap<String, String>();
bindGridview();
return rootView;
}
public void bindGridview(){
new MyAsyncTask(getActivity()).execute("");
}
class MyAsyncTask extends AsyncTask<String, String, String> {
Activity mContex;
public MyAsyncTask(Activity contex) {
this.mContex=contex;
}
@Override
protected String doInBackground(String... strings) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("SELECT_FUNCTION",SELECT_FUNCTION));
params.add(new BasicNameValuePair("ID", ID));
JSONObject json = jsonParser.makeHttpRequest(url,"POST",params);
// Making a request to url and getting response
String jsonStr = json.toString();
Log.d("Response: ", "> " + jsonStr);
//if (jsonStr != null) {
try {
int success = json.getInt(TAG_SUCCESS);
if (success==1){
profileInfos=json.getJSONArray(TAG_PROFILE_INFO);
for (int i = 0; i<profileInfos.length();i++){
JSONObject c = profileInfos.getJSONObject(i);
String name = c.getString(TAG_FIRST_NAME)
profile.put(TAG_NAME,name);
}
}
else{
Intent i = new Intent(getApplicationContext(),DemoObjectFragment.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
catch (JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result){
TextView txt = (TextView) mContex.findViewById(R.id.text1);
txt.setText("Bonjour");
}
}
}
}
提前致谢。 对不起,如果问题与以前的问题类似