0

所以我正在尝试创建一个 Android 应用程序,它基本上根据 UI 内的搜索查询读出 twitter 提要。我需要从解析的 JSON 中显示的提要是用户名、句柄、个人资料图片和推文。

现在我已经创建了整个东西并且我的代码编译了,但是一旦我运行它,应用程序就会打开,我在搜索提要中写一些东西并按 Enter - “不幸的是,AppName 已停止工作”我正在附加我的 logcat 和我的源代码以供参考。

*通过从 DoInBackground 中删除设置文本,然后为 Android 提供足够的权限来访问 Internet,从而解决了该问题。现在的问题是,当我尝试显示个人资料图片时,显示的是 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 回答