2017-03-13 7 views
0

Ich versuche, Daten von Facebook mit Graph Facebook, wie Profile Pic, Gender, Age, Name, ID & Link abrufen, ich bekomme Facebook Profile Pic nur erfolgreich, aber andere werden nicht abgerufen. Bitte helfen Sie!Grafik Facebook wird keine Daten abrufen

MainActivity.java

package com.example.facebookprofile; 

import org.json.JSONException; 
import org.json.JSONObject; 
import com.example.facebookprofile.pictureAsync.onImageDownloaded; 
import com.example.facebookprofile.profileAsync.profileImplement; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements profileImplement, onImageDownloaded { 

    ProgressDialog dialog; 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    public void onClick(View view){ 
     EditText enterUsername = (EditText) findViewById(R.id.enterUsername); 
     String s = "https://graph.facebook.com/" + enterUsername.getEditableText().toString(); 
     pictureAsync picTask = new pictureAsync(this); 
     profileAsync task = new profileAsync(this); 
     picTask.execute(s + "/picture?type=large"); 
     picTask.delegate = this; 
     task.delegate = this; 
     task.execute(s); 


    } 

    public static User parseJSON(String jsonString) throws JSONException{ 
     JSONObject top = new JSONObject(jsonString); 
     String name = "NA"; 
     if(top.has("name")) 
     name = top.getString("name"); 
     String username = top.getString("username"); 
     String id = "NA"; 
     if(top.has("id")) 
     id = top.getString("id"); 
     String gender = "NA"; 
     if(top.has("gender")) 
     gender = top.getString("gender"); 
     String link = "https://www.facebook.com/"+username; 
     if(top.has("link")) 
     link = top.getString("link"); 
     User output = new User(name, username, id, gender, link); 
     return output; 
    } 

    @Override 
    public void update(User user) { 
     // TODO Auto-generated method stub 
     if(user == null){ 
      Toast t = Toast.makeText(this, "Invalid username", Toast.LENGTH_SHORT); 
      t.show(); 
     } 
     else{ 
      TextView name = (TextView) findViewById(R.id.name); 
      TextView id = (TextView) findViewById(R.id.id); 
      TextView gender = (TextView) findViewById(R.id.gender); 
      TextView link = (TextView) findViewById(R.id.link); 
      name.setText(user.name); 
      id.setText(user.id); 
      gender.setText(user.gender); 
      link.setText(user.link); 
     } 
    } 

    @Override 
    public void setImage(Bitmap bitmap) { 
     // TODO Auto-generated method stub 
     if(bitmap != null){ 
      ImageView image = (ImageView) findViewById(R.id.imageView1); 
      image.setImageBitmap(bitmap); 
     } 
    } 
} 

pictureAsync.java

package com.example.facebookprofile; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import javax.net.ssl.HttpsURLConnection; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 

public class pictureAsync extends AsyncTask<String, Integer, Bitmap> { 

    Context context; 
    onImageDownloaded delegate; 

    public interface onImageDownloaded{ 
     public void setImage(Bitmap bitmap); 
    } 
    public pictureAsync(Context context) { 
     this.context = context; 
    } 
    @Override 
    protected Bitmap doInBackground(String... params) { 
     String picUrl = params[0]; 
     try { 
      URL url = new URL(picUrl); 
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Bitmap result) { 
     super.onPostExecute(result); 
     if(delegate!=null) 
     delegate.setImage(result); 
    } 
} 

profileAsync.java

package com.example.facebookprofile; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Scanner; 
import javax.net.ssl.HttpsURLConnection; 
import org.json.JSONException; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask;  

public class profileAsync extends AsyncTask<String, Integer, User> { 

    profileImplement delegate; 
    Context context; 
    ProgressDialog dialog; 


    public profileAsync(Context context) { 
     this.context = context; 
    } 
    @Override 
    public void onPreExecute() { 
     super.onPreExecute(); 
     dialog = new ProgressDialog(context); 
     dialog.setMessage("Loading...Please wait"); 
     dialog.show(); 
    } 

    public interface profileImplement{ 
     public void update(User user); 
    } 

    @Override 

    protected User doInBackground(String... arg) { 
     String user = arg[0]; 
     try { 
      URL url = new URL(user); 
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      if(input == null) return null; 
      Scanner networkScanner = new Scanner(input); 
      StringBuffer buffer = new StringBuffer(); 
      int count = 0; 
      while(networkScanner.hasNext()){ 
       String line = networkScanner.next(); 
       count += line.length(); 
       //publishProgress(count); 
       buffer.append(line); 
      } 
      networkScanner.close(); 
      return MainActivity.parseJSON(buffer.toString()); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(User result) { 
     dialog.dismiss(); 
     if(delegate != null) 
     delegate.update(result); 
    } 

// protected void onProgressUpdate(Integer... values) { 
//  // TODO Auto-generated method stub 
//  super.onProgressUpdate(values); 
//  dialog.setMessage("downloaded: " + values[0]); 
// } 
} 

User.java

package com.example.facebookprofile; 

public class User { 
    String name; 
    String username; 
    String id; 
    String gender; 
    String link; 

    User(String name, String username , String id , String gender , String link){ 
     this.name = name; 
     this.username = username; 
     this.id = id; 
     this.gender = gender; 
     this.link = link; 
    } 
} 
+1

@Martijn Pieters Zeigen Sie mir doppelte Frage, warum es doppelt markiert. –

+1

Sie haben dieselbe Antwort auf mehrere Fragen gepostet. Du dachtest, deine Antwort auf deine eigene Frage habe auch auf die andere geantwortet, also hast du deutlich die Antwort gefühlt, die auf beide angewandt wurde. Ich sehe jetzt, dass deine Antwort zu dünn ist, um diese Schlussfolgerung zu stützen, ich habe diese wieder geöffnet. –

+0

In Zukunft wäre es hilfreich, wenn Sie Ihre Antwort detaillierter machen könnten; wie in welcher Version etwas veraltet ist. –

Antwort

0

Es ist ein Merkmal veraltet gegangen.

Verwandte Themen