从链接调用图像作为数组内的字符串

时间:2014-10-05 14:02:07

标签: java android json eclipse android-imageview

我有json数组包含图像的链接,我想将这些链接中的图像投影到imageView1,我已经成功地将链接作为字符串并在textView上查看它们但我如何从这些链接投影图像?

这是我的MainActivity代码

package learn2crack.listview;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;



import learn2crack.listview.library.JSONParser;



public class MainActivity extends Activity {
ListView list;
TextView ver;
TextView name;
TextView api;
Button Btngetdata;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

//URL to get JSON Array
private static String url = "http://app-chef.com/JsonSC.php";

//JSON Node Names 
private static final String TAG_OS = "android";
//private static final String TAG_VER = "ver";
//private static final String TAG_NAME = "name";
//private static final String TAG_API = "api";
//////////////////////////////////////////////////////////////
private static final String TAG_id          = "id";
private static final String TAG_CITY        ="CITY";
private static final String TAG_CATEGORY    ="CATEGORY";
private static final String TAG_NAME        ="NAME";
private static final String TAG_IMAGELINK   ="IMAGELINK";
private static final String TAG_DESCRIPTION ="DESCRIPTION";
//private static final String TAG_LINKS;
//  private static final String TAG_PHONES;
///private static final String TAG_EMAILS;
/// private static final String TAG_ADDRESS;
//  private static final String TAG_LATITUDE;
//  private static final String TAG_LONGITUDE;


//////////////////////////////////////////////////////////////
JSONArray android = null;




@Override
protected void onCreate(Bundle savedInstanceState) {



    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    oslist = new ArrayList<HashMap<String, String>>();



    Btngetdata = (Button)findViewById(R.id.getdata);
    Btngetdata.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
             new JSONParse().execute();




        }
    });


}



private class JSONParse extends AsyncTask<String, String, JSONObject> {
     private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         ver = (TextView)findViewById(R.id.NAME);
         name = (TextView)findViewById(R.id.DESCRIPTION);
         api = (TextView)findViewById(R.id.IMAGELINK);


        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();





    }

    @Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
     @Override
     protected void onPostExecute(JSONObject json) {
         pDialog.dismiss();
         try {
                // Getting JSON Array from URL
                android = json.getJSONArray(TAG_OS);
                for(int i = 0; i < android.length(); i++){
                JSONObject c = android.getJSONObject(i);

                // Storing  JSON item in a Variable
                String NAME = c.getString(TAG_NAME);
                String DESCRIPTION = c.getString(TAG_DESCRIPTION);
                String IMAGELINK = c.getString(TAG_IMAGELINK);



                // Adding value HashMap key => value


                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_NAME, NAME);
                map.put(TAG_DESCRIPTION, DESCRIPTION);
                map.put(TAG_IMAGELINK, IMAGELINK);

                oslist.add(map);
                list=(ListView)findViewById(R.id.list);





                ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                        R.layout.list_v,
                        new String[] { TAG_NAME,TAG_DESCRIPTION, TAG_IMAGELINK }, new int[] {
                                R.id.NAME,R.id.DESCRIPTION, R.id.IMAGELINK});


                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();

                    }
                });

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


     }
}


   }

这是我的JSONParser.java

   package learn2crack.listview.library;

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

   import org.apache.http.HttpEntity;
   import org.apache.http.HttpResponse;
   import org.apache.http.client.ClientProtocolException;
   import org.apache.http.client.methods.HttpPost;
   import org.apache.http.impl.client.DefaultHttpClient;
   import org.json.JSONException;
   import org.json.JSONObject;

   import android.util.Log;

   public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
}

这是我的activity_main xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/getdata" />

<Button
    android:id="@+id/getdata"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="23dp"
    android:text="Get Data" />

    </RelativeLayout>

和list_v xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/NAME"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/DESCRIPTION"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<TextView
    android:id="@+id/IMAGELINK"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

请注意,我使用list来投影json数组的内容

感谢

0 个答案:

没有答案