2017-08-27 46 views
1

如何从onPostExecute获取json结果到另一个活动?我能够根据登录的用户名从数据库获取用户详细信息。因此,在HomePageActivity中有一个按钮可用于配置文件,当我单击btnprofile时,它会显示当前活动中的文本视图的用户详细信息(HomePageActivity)但我想要做的是获取用户详细信息,然后将其显示到配置文件活动中的新活动。我尝试使用意图,但当我去配置文件活动它什么也没有显示。你能帮我么? :(从onPostExecute显示到另一个活动json数组结果

这里是我的HomePageActivity:

package com.example.androidmp; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.StringTokenizer; 
import android.content.*; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.view.Menu; 
import android.view.View; 
import android.widget.*; 

import com.example.androidmp.User; 
import java.util.ArrayList; 
import java.util.List; 
import com.example.androidmp.User; 
import com.example.androidmp.Poem; 
import java.util.ArrayList; 
import java.util.HashMap; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ListView; 

public class HomePageActivity extends Activity { 


    Button btnfeed, btnprofile; 
    User u = new User(); 
    String Username,Password,Fullname,Email,Location,Bio,uname; 
    String getusername,getpw,getfn,getem,getloc,getb; 
    JSONObject jsonobject; 
    JSONArray jsonarray; 
    ListView listview; 
    ListViewAdapter adapter; 
    ProgressDialog mProgressDialog; 
    ArrayList<HashMap<String, String>> arraylist; 
    HashMap<String, String> hashMap = new HashMap<String, String>(); 
    Intent i; 
    Bundle bundle; 
    private static final String USERNAME = "Username"; 
    private static final String PASSWORD = "Password"; 
    private static final String FULLNAME = "Fullname"; 
    private static final String EMAIL = "Email"; 
    private static final String BIO = "Bio"; 
    private static final String LOCATION = "Location"; 
    String s=""; 
    TextView fn,em,loc,b; 
    private static final String TAG_PROFILE = "user"; 
    private static final String PROFILE_URL = "http://192.168.1.5/webservices/mycontroller/getUser.php"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_homepage); 


     Intent intent = getIntent(); 
     u.SetUsername(intent.getStringExtra(u.username())); 
     fn = (TextView) findViewById(R.id.textView1); 
     em = (TextView) findViewById(R.id.textView2); 
     loc = (TextView) findViewById(R.id.textView3); 
     b = (TextView) findViewById(R.id.textView4); 
     TextView textView = (TextView) findViewById(R.id.getusername); 
     textView.setText(u.getUsername()); 
     uname = u.getUsername().toString(); 
     //uname = textView.toString(); 

     //u.SetUsername(uname.toString()); 

     btnprofile = (Button) findViewById(R.id.btnprofile); 
     btnfeed = (Button) findViewById(R.id.btnfeed); 

     //getem = em.getText().toString(); 
     //getloc = loc.getText().toString(); 
     //getb = b.getText().toString(); 

     btnprofile.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 


       new ProfileAsync().execute(); 


       Intent i = new Intent(HomePageActivity.this,ProfileActivity.class); 

       i.putExtra("fullname",Fullname); 
       i.putExtra("email", Email); 
       i.putExtra("bio", Bio); 
       i.putExtra("location", Location); 
       startActivity(i); 
       finish(); 



      } 
     }); 

     btnfeed.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent a = new Intent(HomePageActivity.this,FeedActivity.class); 
       startActivity(a); 
      } 
     }); 
    } 


     class ProfileAsync extends AsyncTask<String, String, String>{ 

       private Dialog loadingDialog; 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        loadingDialog = ProgressDialog.show(HomePageActivity.this, "Please wait", "Loading..."); 
       } 

       @Override 
       protected String doInBackground(String... params) { 
        String json=null; 
        byte[] data; 
        StringBuffer buffer = null; 
        InputStream is = null; 



        try{ 
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
         nameValuePairs.add(new BasicNameValuePair("Username", uname)); 

         HttpClient httpClient = new DefaultHttpClient(); 
         HttpPost httpPost = new HttpPost(PROFILE_URL); 
         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

         HttpResponse response = httpClient.execute(httpPost); 

         HttpEntity entity = response.getEntity(); 
         json = EntityUtils.toString(entity); 
         Log.e("Profile JSON: ", json.toString()); 





        } catch (ClientProtocolException e) { 
         e.printStackTrace(); 
        } catch (UnsupportedEncodingException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        return json; 
       } 

       @Override 
       protected void onPostExecute(String json){ 
        super.onPostExecute(json); 

        loadingDialog.dismiss(); 

        try 
        { 
        jsonobject = new JSONObject(json); 
        jsonarray = jsonobject.getJSONArray("user"); 
        JSONObject jb= jsonarray.getJSONObject(0); 
        //Username = jb.getString("Username"); 
        //Password = jb.getString("Password"); 
        Fullname = jb.getString("Fullname"); 
        Email = jb.getString("Email"); 
        Bio = jb.getString("Bio"); 
        Location = jb.getString("Location"); 


        fn.setText(Fullname); 
        em.setText(Email); 
        loc.setText(Location); 
        b.setText(Bio); 



        }catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 



       } 
      }//end of asynctask 






} 

这里是我的ProfileActivity:

package com.example.androidmp; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.StringTokenizer; 
import android.content.*; 
import com.example.androidmp.User; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.view.Menu; 
import android.view.View; 
import android.widget.*; 


public class ProfileActivity extends Activity { 


    Button btncreate; 
    private TextView _username,_password,_fullname,_Email,_bio,_location; 
    User u = new User(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.profileview); 

     //_username = (TextView) findViewById(R.id._username); 
     //_password = (TextView) findViewById(R.id._password); 
     _fullname = (TextView) findViewById(R.id._fullname); 
     _Email = (TextView) findViewById(R.id._Email); 
     _bio = (TextView) findViewById(R.id._bio); 
     _location = (TextView) findViewById(R.id._location); 


     Intent i = getIntent(); 
     u.SetUsername(i.getStringExtra(u.username())); 
     _username.setText(u.getUsername()); 




     String displayfullname = i.getExtras().getString("fullname"); 
     String displayemail = i.getExtras().getString("email"); 
     String displaybio = i.getExtras().getString("bio"); 
     String displaylocation = i.getExtras().getString("location"); 


     _fullname.setText(displayfullname); 
     _Email.setText(displayemail); 
     _bio.setText(displaybio); 
     _location.setText(displaylocation); 



    } 




} 

回答

1

这是因为新的活动开始完成异步任务之前只需拨打功能开始在新的活动。 onPostExecute()函数

+0

谢谢先生,我的代码现在正在工作! :) –

+0

不客气,你能把这个标记为接受吗? :) –

0

获取详细信息后传递值settext是否正常工作?那么您可以给出该范围的意图, e onPostexecute方法。在做这件事之前请看。 Look this

+0

谢谢,先生!我将ontent放在onPostExecute方法中,它现在可以工作了! –

+0

很高兴你做到了:) – Manoranjan

相关问题