0

だから私は基本的に UI 内の検索クエリに従って Twitter フィードを読み取る Android アプリを作成しようとしています。解析された JSON から表示する必要があるフィードは、ユーザー名、ハンドル、プロフィール写真、ツイートです。

これですべてが作成され、コードがコンパイルされましたが、実行するとすぐにアプリが開き、検索フィードに何かを書いてEnterキーを押します-「残念ながら、AppNameは機能しなくなりました」logcatとソースコードを添付しています参考のため。

* DoInBackground からセット テキストを削除し、Android がインターネットにアクセスするための適切な許可を与えることで問題を解決しました。問題は、プロフィール写真を表示しようとすると、画像ではなく URL が表示されることです。

ソースコード :

package com.example.twittersearchactivity;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
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.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class TwitterSearchActivity extends Activity {

    private TextView tweetDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_twitter_search);
        tweetDisplay = (TextView)findViewById(R.id.tweet_txt);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.twitter_search, menu);
        return true;
    }
    public void searchTwitter(View view){
    EditText searchTxt = (EditText)findViewById(R.id.search_edit);
    String searchTerm = searchTxt.getText().toString();
    if(searchTerm.length()>0){
        try{
            String encodedSearch = URLEncoder.encode(searchTerm, "UTF-8");
            String searchURL = "http://search.twitter.com/search.json?q="+encodedSearch;
            new GetTweets().execute(searchURL);
            Log.i("1", "entered the searchterm");
        }
        catch(Exception e){
            tweetDisplay.setText("Whoops - something went wrong!");
            e.printStackTrace();
        }
    }
    else
        tweetDisplay.setText("Enter a search query!");
    }
    private class GetTweets extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... twitterURL) {
            StringBuilder tweetFeedBuilder = new StringBuilder();
            for (String searchURL : twitterURL) {
                HttpClient tweetClient = new DefaultHttpClient();
                try {
                    HttpGet tweetGet = new HttpGet(searchURL);
                    HttpResponse tweetResponse = tweetClient.execute(tweetGet);
                    StatusLine searchStatus = tweetResponse.getStatusLine();

                    if (searchStatus.getStatusCode() == 200) {
                        HttpEntity tweetEntity = tweetResponse.getEntity();
                        Log.i("2", "entered gettweets");
                        InputStream tweetContent = tweetEntity.getContent();
                        InputStreamReader tweetInput = new InputStreamReader(tweetContent);
                        BufferedReader tweetReader = new BufferedReader(tweetInput);
                        String lineIn;
                        while ((lineIn = tweetReader.readLine()) != null) {
                            tweetFeedBuilder.append(lineIn);
                            Log.i("3", "entered while in dobackground");
                        }
                    }
                    else {Log.i("error", "error");}
                        //tweetDisplay.setText("Whoops - something went wrong!");
                }
                catch(Exception e) {
                    Log.e("DEBUGTAG", "Remote Image Exception", e);
                    //tweetDisplay.setText("Whoops - something went wrong!");
                    e.printStackTrace();
                }}

            return tweetFeedBuilder.toString();
        }
        protected void onPostExecute(String result) {
            StringBuilder y;
            StringBuilder tweetResultBuilder = new StringBuilder();
            try {
                Log.i("tag", "entered try block");
                JSONObject resultObject = new JSONObject(result);
                JSONArray tweetArray = resultObject.getJSONArray("results");
                for (int t=0; t<tweetArray.length(); t++) {
                    Log.i("tag", "entered the json stream");
                    JSONObject tweetObject = tweetArray.getJSONObject(t);
                    tweetResultBuilder.append(tweetObject.getString("from_user")+": ");
                    tweetResultBuilder.append(tweetObject.getString("from_user_name")+": ");
                    tweetResultBuilder.append(tweetObject.get("text")+"\n\n");
                    String imageURL = (String) tweetObject.get(("profile_image_url")+": ");
                    Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
                    @SuppressWarnings("deprecation")
                    Drawable d =new BitmapDrawable(bitmap);
                    d.setAlpha(255);
                    TextView.setCompoundDrawablesWithIntrinsicBounds(0,0,1,0);
                   }
            }
            catch (Exception e) {
                tweetDisplay.setText("Whoops - something went wrong!");
                e.printStackTrace();}

            if(tweetResultBuilder.length()>0)
                tweetDisplay.setText(tweetResultBuilder.toString());
            else
                tweetDisplay.setText("Sorry - no tweets found for your search!");
        }
    }}
4

1 に答える 1

2

setText関数のような別のスレッドのようにビュー関数を呼び出すことはできませんAsyncTask doInBackground。で行う必要がありますonPostExecute

于 2013-02-26T06:41:19.943 に答える