我是初级 Android 开发人员,我正在使用 JSON 从 Web 服务器上的 PHP 文件中获取数据,该文件从 MySql 数据库中获取数据
这是我的代码
public void onlinegetbooks() {
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user",userid));
HttpClient httpclient = new DefaultHttpClient();
try{
HttpPost httppost = new HttpPost("http://url/mybooks.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
//buffered reader
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 80);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
resultt = sb.toString();
System.out.println("in onlinegetbooks, resultt: "+resultt);
System.out.println("onlinegetbooks StringBuilder: "+sb);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "invalid", Toast.LENGTH_LONG).show();
}
resultt = resultt.substring(1);
jArray = new JSONArray(resultt);
System.out.println("in onlinegetbooks jArray.length(): "+jArray.length());
try{
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
targetcover=json_data.getString("bo_cover_img");
targetbname = json_data.getString("bo_name");
targetbpath = json_data.getString("bo_path");
targetbpartion=json_data.getString("bo_bg_id");
targetauthname=json_data.getString("au_name");
targetbokid=json_data.getString("bo_id");
targetbokabout=json_data.getString("bo_about");
coverr.add(targetcover);
bnamee.add(targetbname);
bpathh.add(targetbpath );
bpartionnn.add(targetbpartion);
authnamee.add(targetauthname);
bokiddd.add(targetbokid);
bokabouttt.add(targetbokabout);
}
fflage=true;
}catch(JSONException e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
} catch (ParseException e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
我在下面的代码中调用这个函数
private class LoadData extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
CharSequence contentTitle = getString(R.string.loginning);
this.progressDialog = ProgressDialog.show(loginActivity.this,"",contentTitle);
}
@Override
protected void onPostExecute(final Void unused) {
this.progressDialog.dismiss();
if(fflage){
Intent myBooks = new Intent(getApplicationContext(),TabsOFBooks.class);
myBooks.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myBooks);
finish();
} else {
Toast.makeText(getApplicationContext(),R.string.noinfo, Toast.LENGTH_LONG).show();
}
}
@Override
protected Void doInBackground(Void... params) {
try{
bk.deletetable();
recb.deletetable();
mosb.deletetable();
frboks.deletetable();
ggp.deletetable();
addusers();
onlinegetbooks();
addmybooks();
onlinerecentbooks();
fflage=true;
}
catch (Exception e) {
System.out.println("No Information");
}
return null;
}
}
我的问题在resultt = sb.toString();
当不在继续代码中的其他行并且不打印此resultt
行时Null
System.out.println("in onlinegetbooks jArray.length(): "+jArray.length());