2012-12-22 28 views
0

嗨,這是我的第一個應用程序。 我是一個初學者,當談到編程和android。 我的應用程序類似於twitter應用程序。但不是從twitter獲取值,它會從我的數據庫中獲取值。該應用程序將加載圖像和鏈接到網頁。Android - Json和TwitterFeed代碼

我的問題。 比方說,我的應用程序在應用程序商店,並有人下載它。 我的應用程序通過Json從我的mysql數據庫獲取大部分內容。每當我向數據庫添加新內容時,用戶都必須更新應用程序以獲取新內容,否則它會自動更新自己的內容。

有人可以看看我的代碼。我不清楚Async()和doInBackground()。如果您有任何其他有用的編碼建議,請讓我知道。我想在應用商店完成時將此應用放在應用商店中。

親切的問候

public class TwitterFeedActivity extends ListActivity { 

    private ArrayList<Tweet> tweets = new ArrayList<Tweet>(); 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     new MyTask().execute(); 
    } 
    private class MyTask extends AsyncTask<Void, Void, Void> { 
     private ProgressDialog progressDialog; 


     protected void onPreExecute() { 
      progressDialog = ProgressDialog.show(TwitterFeedActivity.this, 
        "", "Loading. Please wait...", true); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      try { 
       HttpClient hc = new DefaultHttpClient(); 
       HttpGet get = new HttpGet("http://myurl.com"); 
       HttpResponse rp = hc.execute(get); 

       if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
       { 
        String result = EntityUtils.toString(rp.getEntity()); 
        JSONObject root = new JSONObject(result); 
        JSONArray sessions = root.getJSONArray("results"); 

        for (int i = 0; i < sessions.length(); i++) { 
         JSONObject session = sessions.getJSONObject(i); 
         Tweet tweet = new Tweet(); 
         tweet.n_text = session.getString("n_text"); 
         tweet.n_name = session.getString("n_name"); 
         tweet.Image = session.getString("Image"); 
         tweets.add(tweet); 
        } 
       } 

      } catch (Exception e) { 
       Log.e("TwitterFeedActivity", "Error loading JSON", e); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      progressDialog.dismiss(); 
      setListAdapter(new TweetListAdaptor(TwitterFeedActivity.this, R.layout.list_item, tweets)); 
     } 
    } 

    private class TweetListAdaptor extends ArrayAdapter<Tweet> { 
     private ArrayList<Tweet> tweets; 
     public TweetListAdaptor(Context context, int textViewResourceId, ArrayList<Tweet> items) { 
      super(context, textViewResourceId, items); 
      this.tweets = items; 
     } 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.list_item, null); 
      } 
      final Tweet o = tweets.get(position); 
      TextView tt = (TextView) v.findViewById(R.id.toptext); 
      TextView bt = (TextView) v.findViewById(R.id.bottomtext); 
      ImageView image = (ImageView) v.findViewById(R.id.avatar); 
      bt.setText(o.n_name); 
      tt.setText(o.n_text); 
      image.setImageBitmap(getBitmap(o.Image)); 

      image.setOnClickListener(new View.OnClickListener(){ 
       public void onClick(View v){ 
        Intent intent = new Intent(); 
        intent.setAction(Intent.ACTION_VIEW); 
        intent.addCategory(Intent.CATEGORY_BROWSABLE); 
        intent.setData(Uri.parse(o.n_text)); 
        startActivity(intent); 
       } 
      }); 

      return v; 
     } 
    } 

    public Bitmap getBitmap(String bitmapUrl) { 
     try { 
      URL url = new URL(bitmapUrl); 
      return BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
     } 
     catch(Exception ex) {return null;} 
    } 
} 
+0

首先確保你是從高音揚聲器服務器得到響應或不在doInBackground中? –

回答

0

好吧我張貼的異步相關的代碼。我希望,你會發現它的幫助。首先,做一個類,如下所示:

package com.example.pre; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 




import android.util.Log; 

public class JSONFunctions { 

    public String CreateCon(String url){ 
     String res=""; 
     try{ 
      URL u=new URL(url); 
      HttpURLConnection con = (HttpURLConnection) u. 
        openConnection(); 

       res=readStream(con.getInputStream()); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }  




     return res; 
    } 

    private String readStream(InputStream is) { 
     // TODO Auto-generated method stub 
     String result = ""; 
     BufferedReader reader=null; 
      try{ 
       reader = new BufferedReader(new InputStreamReader(is)); 

       String line = null; 
       while ((line = reader.readLine()) != null) { 
         result=result+line; 
       } 
      }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     }finally{ 
      if(reader!=null) 
      { 
       try { 
        reader.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
     return result; 
    } 
} 

其次在主類中,粘貼下面的方法:

private class PostTask extends AsyncTask<String, Integer, String>{ 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     readJson(); 
      //progress_dialog.dismiss(); 
    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
    } 
    @Override 
    protected void onProgressUpdate(Integer... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 
    } 
    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     JSONFunctions jsonf=new JSONFunctions(); 
     str1=jsonf.CreateCon(cat_url); 
     return null; 
    } 
} 

其中readJson是你定義的功能,並且str1方法是字符串你在哪裏提供json的地址。不要忘記調用PostExecute類。