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 回答