0

ユーザープロフィールページを作ろうとしています。

私はいくつかのハードワークを行い、jsonオブジェクトを取得してユーザーデータをsqlデータベースに保存し、これらのデータ(文字列)を名前や電子メールなどのTextViewに表示することができました。

同じデータベースにユーザーの写真への URL もありますが、他のテキスト文字列と同じように、URL を取得して ImageView に表示することはできません。

誰かがこのコードを微調整してそれを行う方法を教えてもらえますか?

Java初心者からこれを考慮してください。ありがとうございました!^^

public class DirectoryDetailMeActivity extends Activity {

String eid; //user ID

JSONParser jsonParser = new JSONParser();

private static final String url_veiw_directory = "http://www.myweb.com/android/include/directory_detail_me.php";

private static final String TAG_ID = "eid";
private static final String TAG_IMG = "photo"; 
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";    

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);         
setContentView(R.layout.directory_detail_me);

new GetDirectoryDetails().execute();

}

class GetDirectoryDetails extends AsyncTask<String, String, String> {

protected String doInBackground(String... params) {     

runOnUiThread(new Runnable() {
    public void run() {

    int success;
        try {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("id", eid));

            JSONObject json = jsonParser.makeHttpRequest(url_veiw_directory, "GET", params);

            Log.d("my profile", json.toString());

            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {

            JSONArray directoryObj = json.getJSONArray(TAG_DIRECTORY); 
            JSONObject directory = directoryObj.getJSONObject(0);

            // It works fine if I give the URL manually. 
            String imageURL = "";

            ImageView imagePhoto = (ImageView) findViewById(R.id.photo);    

            ImageLoader imgLoader = new ImageLoader(getApplicationContext());        
            imgLoader.DisplayImage(imageURL, imagePhoto);

            TextView txtName = (TextView) findViewById(R.id.name);
            TextView txtEmail = (TextView) findViewById(R.id.email);                    

            txtName.setText(directory.getString(TAG_NAME));
            txtEmail.setText(directory.getString(TAG_EMAIL));

            }else{

        }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
4

1 に答える 1

4

JSONObject から URL を取得して imageURL を埋める必要があります。JSONObject には画像配列があると思います。

まずはこれを試してみてください。

  String imageURL = directory.getString(TAG_IMG);
  //then
  ImageView imagePhoto = (ImageView) findViewById(R.id.photo);    
  ImageLoader imgLoader = new ImageLoader(getApplicationContext());        
  imgLoader.DisplayImage(imageURL, imagePhoto);

うまくいかない場合

 JSONArray imageObj = directory.getJSONArray(TAG_IMG); 
 JSONObject img = imageObj.getJSONObject(0);
 String imageURL = img.getString("NewTAG");
  //then
 ImageView imagePhoto = (ImageView) findViewById(R.id.photo);    
 ImageLoader imgLoader = new ImageLoader(getApplicationContext());        
 imgLoader.DisplayImage(imageURL, imagePhoto);

どちらも駄目なら。ImageLoder クラスで、この行を変更します

 InputStream is = (InputStream) new URL(url).getContent();

これに

 InputStream is = (InputStream) new URL(url).openConnection().getInputStream();
于 2012-11-16T16:05:54.457 に答える