2014-12-22 3 views
1

저는 Twitter API 1.1을 사용하여 다양한 채널에서 트윗을 가져옵니다. 지난 3 일간 나는 Proguard를 활성화하여 Apk를 줄이는 과정을 모두 마쳤습니다. 하지만 그것은 에뮬레이터가 아닌 DEVICE의 트윗을 가져 오는 중에 앱으로 중지됩니다. FragmentOne.java의 실행Proguard가 장치에서 릴리스 앱을 실행할 수 없도록 설정합니다.

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.net.URLEncoder; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.client.methods.HttpRequestBase; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.BasicHttpParams; 

import android.annotation.SuppressLint; 
import android.app.AlertDialog; 
import android.app.ListFragment; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Base64; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 
import com.google.gson.Gson; 
import com.mypackage.facts.R; 
import com.mypackage.twitter.Authenticated; 
import com.mypackage.twitter.SingleListItem; 
import com.mypackage.twitter.Tweet; 
import com.mypackage.twitter.Twitter; 
import com.mypackage.twitter.TwitterUser; 

@SuppressLint("NewApi") 
public class FragmentOne extends ListFragment { 
    public boolean net; 
    ImageView ivIcon; 
    TextView tvItemName; 
    final static String LOG_TAG = "rnc"; 
    ListView listview; 
     String product; 
     AdView adView; 
    // private InterstitialAd interstitial; 
    public FragmentOne() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.twit_list, container, 
       false); 

     downloadTweets(); 


     return view; 

    } 

     @Override 
      public void onListItemClick(ListView l, View v, int position, long id) { 
       Log.i("FragmentList2", "Item clicked: " + id); 

       Object selectedValue = this.getListAdapter().getItem(position); 
       product = selectedValue.toString(); 

      //  System.out.println("lota "+product); 

       Intent intent = new Intent(FragmentOne.this.getActivity(), SingleListItem.class); 
       intent.putExtra("product", product); 
      // Toast.makeText(getActivity(), item, Toast.LENGTH_LONG).show(); 
       startActivity(intent); 
      } 


    public void downloadTweets() { 
     TwitterUser o = new TwitterUser(); 
     String m = o.getValue(); 

     System.out.println("Kida "+m); 


    // listview = this.getListView(); 



     String ScreenName =m; 

     ConnectivityManager connMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

     if (networkInfo != null && networkInfo.isConnected()) { 
      new DownloadTwitterTask().execute(ScreenName); 
     } else { 
      Log.v(LOG_TAG, "No network connection available."); 
     } 
    } 



    // Uses an AsyncTask to download a Twitter user's timeline 
     private class DownloadTwitterTask extends AsyncTask<String, Void, String> { 
      final static String CONSUMER_KEY = "3GCPNkJ9cZesXIBDaMyYHDkqU"; 
      final static String CONSUMER_SECRET = "sPxxPRfXGonaIcWT6r5GbWwU4cSZWW0tmXv2HD5VX3RDYkCoDG"; 
      final static String TwitterTokenURL = "https://api.twitter.com/oauth2/token"; 
      final static String TwitterStreamURL = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name="; 


      private ProgressDialog progressDialog; 
      @Override 
      // can use UI thread here 
      protected void onPreExecute() { 
      //this.progressDialog = ProgressDialog.show(Boys.this, ""," Look whose back !! Ok Let me see what i have for you "); 
       try{ 
       progressDialog = new ProgressDialog(FragmentOne.this.getActivity(),AlertDialog.THEME_HOLO_DARK); 
       progressDialog.setIndeterminate(true); 
       progressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.loader_2)); 
       progressDialog.setMessage("Have Some Patience Please..."); 
       progressDialog.show(); 
      // progressDialog.setCancelable(false); 
      // progressDialog.setCanceledOnTouchOutside(false); 
       } 
       catch(Exception e) 
       { 
        this.progressDialog.dismiss(); 
        Toast.makeText(getActivity().getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show(); 

       } 
      } 

      @Override 
      protected String doInBackground(String... screenNames) { 
       String result = null; 

       if (screenNames.length > 0) { 
        result = getTwitterStream(screenNames[0]); 
       } 
       return result; 
      } 

      // onPostExecute convert the JSON results into a Twitter object (which is an Array list of tweets 
      @Override 
      protected void onPostExecute(String result) { 
       Twitter twits = jsonToTwitter(result); 

       // lets write the results to the console as well 
       for (Tweet tweet : twits) { 
        Log.i(LOG_TAG, tweet.getText()); 
       } 

       System.out.println("Kamina "+ twits); 

       // send the tweets to the adapter for rendering 

        ArrayAdapter<Tweet> adapter = new ArrayAdapter<Tweet>(getActivity().getBaseContext(),R.layout.customgrid,R.id.texts, twits); 
        setListAdapter(adapter); 

        showAd(); 
       this.progressDialog.dismiss(); 
      } 

      // converts a string of JSON data into a Twitter object 
      private Twitter jsonToTwitter(String result) { 
       Twitter twits = null; 
       if (result != null && result.length() > 0) { 
        try { 
         Gson gson = new Gson(); 
         twits = gson.fromJson(result, Twitter.class); 
        } catch (IllegalStateException ex) { 
         // just eat the exception 
        } 
       } 
       return twits; 
      } 

      // convert a JSON authentication object into an Authenticated object 
      private Authenticated jsonToAuthenticated(String rawAuthorization) { 
       Authenticated auth = null; 
       if (rawAuthorization != null && rawAuthorization.length() > 0) { 
        try { 
         Gson gson = new Gson(); 
         auth = gson.fromJson(rawAuthorization, Authenticated.class); 
        } catch (IllegalStateException ex) { 
         // just eat the exception 
        } 
       } 
       return auth; 
      } 

      private String getResponseBody(HttpRequestBase request) { 
       StringBuilder sb = new StringBuilder(); 
       try { 

        DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams()); 
        HttpResponse response = httpClient.execute(request); 
        int statusCode = response.getStatusLine().getStatusCode(); 
        String reason = response.getStatusLine().getReasonPhrase(); 

        if (statusCode == 200) { 

         HttpEntity entity = response.getEntity(); 
         InputStream inputStream = entity.getContent(); 

         BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
         String line = null; 
         while ((line = bReader.readLine()) != null) { 
          sb.append(line); 
         } 
        } else { 
         sb.append(reason); 
        } 
       } catch (UnsupportedEncodingException ex) { 
       } catch (ClientProtocolException ex1) { 
       } catch (IOException ex2) { 
       } 
       return sb.toString(); 
      } 

      private String getTwitterStream(String screenName) { 
       String results = null; 

       // Step 1: Encode consumer key and secret 
       try { 
        // URL encode the consumer key and secret 
        String urlApiKey = URLEncoder.encode(CONSUMER_KEY, "UTF-8"); 
        String urlApiSecret = URLEncoder.encode(CONSUMER_SECRET, "UTF-8"); 

        // Concatenate the encoded consumer key, a colon character, and the 
        // encoded consumer secret 
        String combined = urlApiKey + ":" + urlApiSecret; 

        // Base64 encode the string 
        String base64Encoded = Base64.encodeToString(combined.getBytes(), Base64.NO_WRAP); 

        // Step 2: Obtain a bearer token 
        HttpPost httpPost = new HttpPost(TwitterTokenURL); 
        httpPost.setHeader("Authorization", "Basic " + base64Encoded); 
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 
        httpPost.setEntity(new StringEntity("grant_type=client_credentials")); 
        String rawAuthorization = getResponseBody(httpPost); 
        Authenticated auth = jsonToAuthenticated(rawAuthorization); 

        // Applications should verify that the value associated with the 
        // token_type key of the returned object is bearer 
        if (auth != null && auth.token_type.equals("bearer")) { 

         // Step 3: Authenticate API requests with bearer token 
         HttpGet httpGet = new HttpGet(TwitterStreamURL + screenName+"&count=10"); 

         // construct a normal HTTPS request and include an Authorization 
         // header with the value of Bearer <> 
         httpGet.setHeader("Authorization", "Bearer " + auth.access_token); 
         httpGet.setHeader("Content-Type", "application/json"); 
         // update the results with the body of the response 
         results = getResponseBody(httpGet); 
        } 
       } catch (UnsupportedEncodingException ex) { 
       } catch (IllegalStateException ex1) { 
       } 
       return results; 
      } 
     } 


     void showAd() 
      { 



       // Prepare the Interstitial Ad 
      // interstitial = new InterstitialAd(getActivity()); 
       // Insert the Ad Unit ID 
      // interstitial.setAdUnitId("ca-app-pub-2123194616447553/4834655305"); 

       //Locate the Banner Ad in activity_main.xml 
       AdView adView = (AdView) this.getView().findViewById(R.id.adViews); 

       // Request for Ads 
       AdRequest adRequest = new AdRequest.Builder() 

       // Add a test device to show Test Ads 
       .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
       .addTestDevice("Ku") 
         .build(); 

       // Load ads into Banner Ads 
       adView.loadAd(adRequest); 


        // Load ads into Interstitial Ads 
      // interstitial.loadAd(adRequest); 

       // Prepare an Interstitial Ad Listener 
      /* interstitial.setAdListener(new AdListener() { 
        public void onAdLoaded() { 
         // Call displayInterstitial() function 
         displayInterstitial(); 
        } 
       });*/ 

      } 

     /* public void displayInterstitial() { 
       // If Ads are loaded, show Interstitial else show nothing. 
       if (interstitial.isLoaded()) { 
        interstitial.show(); 
       } 
      }*/ 


} 

난독 FILES 동안

도와주세요

project.properties

# This file is automatically generated by Android Tools. 
# Do not modify this file -- YOUR CHANGES WILL BE ERASED! 
# 
# This file must be checked in Version Control Systems. 
# 
# To customize properties used by the Ant build system edit 
# "ant.properties", and override values to adapt the script to your 
# project structure. 
# 
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 

# Project target. 
target=android-19 
android.library.reference.1=..\\appcompat_v7 
android.library.reference.2=../google-play-services_lib 

난독 화 - project.txt

+0

크래시가 있습니까? 그렇다면 logcat을 게시하십시오. 그리고 proguard txt 파일을 보여주십시오 –

+0

잘 때 장치에 실행하면 충돌이 발생합니다. logcat의 보고서 없음 – Creator

답변

관련 문제