有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将链接中的图像作为数组中的字符串调用

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

这是我的主要活动代码

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 安卓.app.Activity;
import 安卓.app.ProgressDialog;
import 安卓.graphics.Bitmap;
import 安卓.graphics.BitmapFactory;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.Button;
import 安卓.widget.ImageView;
import 安卓.widget.ListAdapter;
import 安卓.widget.ListView;
import 安卓.widget.SimpleAdapter;
import 安卓.widget.TextView;
import 安卓.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 = "安卓";
//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 安卓 = 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
                安卓 = json.getJSONArray(TAG_OS);
                for(int i = 0; i < 安卓.length(); i++){
                JSONObject c = 安卓.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。爪哇

   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 安卓.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;

}
}

这是我的主要xml

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

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

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

    </RelativeLayout>

并列出xml

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

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

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

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

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

</LinearLayout>

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

谢谢


共 (0) 个答案