我试图在Android应用程序中显示json内容,但它不是在阅读它。 我希望它在第一页的json文件中获取“statusmessage”并将其发送到下一页并使用sharedpefrences显示。 但它返回“anon”作为值而不是“Hi +2348055550055,将收取N20.00的访问费用以访问此平台”。在mainactivity.java中的对话框中。我没有错误消息,它不仅仅是返回值。
这是json文件
{headers:
[{
id: "4",
name: "GLO",
code: "GLO",
status: "Inactive",
statusMessage: "Hi +2348055550055, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
}]
}
这是我的主要java类
package com.mall.first;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class ActivitySplash extends Activity {
private ProgressDialog pDialog;
int flag=0;
JSONParser jsonParser = new JSONParser();
private static final String api = "http://10.0.2.2/angl/com.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
ImageView image = (ImageView)findViewById(R.id.image1);
Picasso.with(getBaseContext()).load("http://api.dobox.tv/img/gloSplashScreen.png").into(image);
new CountDownTimer(5000,1000) {
@Override
public void onFinish() {
new loginAccess().execute();
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
finish();
}
@Override
public void onTick(long millisUntilFinished) {
}
}.start();
}
class loginAccess extends AsyncTask<String, String, Boolean> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ActivitySplash.this);
pDialog.setMessage("Login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(String... arg0) {
// TODO Auto-generated method stub
// Check for success tag
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
Log.d("request!", "starting");
JSONObject json = jsonParser.getJSONFromUrl(api);
// check your log for json response
Log.d("Login attempt", json.toString());
JSONArray categories = json.getJSONArray("headers");
for (int i = 0; i < categories.length(); i++) {
String state = categories.getJSONObject(i).getString("state");
String status = categories.getJSONObject(i).getString("status");
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(ActivitySplash.this);
SharedPreferences.Editor edit = sp.edit();
edit.putString("state", state);
edit.putString("status", status);
edit.commit();
// Close all views
}
}catch (Throwable e){
e.printStackTrace();
}
return true;
}
protected void onPostExecute(Boolean file_url) {
pDialog.dismiss();
if(flag==1)
Toast.makeText(ActivitySplash.this,"Please Enter Correct informations", Toast.LENGTH_LONG).show();
}
}
}
这是我的第二堂课
package com.mall.first;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends SherlockFragmentActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String[] titl;
Button pend;
int[] icon;
String pic;
Fragment menus = new Menus();
TextView mystatu,myuser,nick,status;
public static final String PREFS_NAME = "LoginPrefs";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_main);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
status = sp.getString("status", "anon");
root();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public void root(){
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.prompts, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptsView);
alertDialogBuilder
.setCancelable(false)
.setMessage(status)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(MainActivity.this, "payment has been done", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.exit(0);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
答案 0 :(得分:0)
CountDownTimer-&gt; OnFinish中的代码是错误的,因为您在完成asynctask之前打开了intent。您应该将意图移动到asynctask的OnPostExecute方法。这样,您可以确保在打开MainActivity之前在sharedPreferences上写入状态。
Base& Base::base=Base::getBaseInstance();