得到Json的 字节输入流,Byte数组输出流,JSON的字符串。
http://cdefgab.web.fc2.com/song.json JSON文件样本。
package com.example.java.m1115a.Tools;
import android.content.Context;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by java on 2016/11/15.
*/
public class HttpTools {
private String url_path;
private Context context;
public HttpTools(String url_path) {
this.url_path = url_path;
}
public HttpTools(String url_path, Context context) {
this.url_path = url_path;
this.context = context;
}
public InputStream getnetInputStream() throws Exception {
URL url = new URL(url_path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
InputStream inputStream = null;
if (httpURLConnection.getResponseCode() == 200) {
inputStream = httpURLConnection.getInputStream();
}
return inputStream;
}
public ByteArrayOutputStream getbyteArrayOutputStream() throws Exception {
InputStream inputStream = getnetInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = 0;
while ((len = inputStream.read(buff)) != -1) {
byteArrayOutputStream.write(buff, 0, len);
}
inputStream.close();
return byteArrayOutputStream;
}
public String getjsonString() throws Exception {
ByteArrayOutputStream byteArrayOutputStream = getbyteArrayOutputStream();
return byteArrayOutputStream.toString();
}
}
继承HttpTools类
得到JSON的数字HashMap,得到Item的图片URL,得到图片的Bitmap
package com.example.java.m1115a.Tools;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
/**
* Created by java on 2016/11/15.
*/
public class GetJson extends HttpTools {
public GetJson(String url_path) {
super(url_path);
}
public HashMap<Integer, HashMap> jsonMap() throws Exception {
String jsonString = getjsonString();
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject jsonObject1 = jsonObject.getJSONObject("photos");
JSONArray jsonArray = jsonObject1.getJSONArray("photo");
HashMap<Integer, HashMap> jMap = new HashMap<Integer, HashMap>();
for (int i = 0; i < jsonArray.length(); i++) {
HashMap<String, String> item = new HashMap<String, String>();
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Iterator<String> iterator = jsonObject2.keys();
while (iterator.hasNext()) {
String temp = iterator.next();
item.put(temp, jsonObject2.getString(temp));
}
jMap.put(i, item);
}
return jMap;
}
public static String img_url(HashMap<String, String> item_hashMap) throws Exception {
/*
*
* https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
* https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
* https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)
*
* String farmid = item_hashMap.get("farm");
* String Serverid = item_hashMap.get("server");
* String id = item_hashMap.get("id");
* String secret = item_hashMap.get("secret");
* String img_url = "https://farm" + farmid + ".staticflickr.com/" + Serverid + "/" + id + "_" + secret + ".jpg";
* System.out.println(img_url);
*/
HashMap<String, String> temp_map = item_hashMap;
return "https://farm" + item_hashMap.get("farm") + ".staticflickr.com/" + item_hashMap.get("server") + "/" + item_hashMap.get("id") + "_" + item_hashMap.get("secret") + ".jpg";
}
public static Bitmap getBitmap(String img_url) throws Exception {
URL url = new URL(img_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
Bitmap bitmap = null;
if (httpURLConnection.getResponseCode() == httpURLConnection.HTTP_OK) {
InputStream inputStream = httpURLConnection.getInputStream();
if (inputStream != null) {
byte[] buf = new byte[1024];
int len = 0;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(buf)) != -1) {
byteArrayOutputStream.write(buf, 0, len);
byteArrayOutputStream.flush();
}
bitmap = BitmapFactory.decodeByteArray(byteArrayOutputStream.toByteArray(), 0, byteArrayOutputStream.size());
inputStream.close();
byteArrayOutputStream.close();
inputStream.close();
byteArrayOutputStream.close();
}
}
return bitmap;
}
}
完成的例子,测试
每按一次按键,更换下一张图片。
应该不需要每次后 New URL.
package com.example.java.m1115a;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.java.m1115a.Tools.GetJson;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
private int cont;
@Override
public void onClick(View v) {
new img().execute("http://cdefgab.web.fc2.com/song.json", String.valueOf(cont));
cont++;
}
});
}
public class img extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
GetJson json = new GetJson(params[0]);
int cont = Integer.parseInt(params[1]);
Bitmap bitmap = null;
try {
bitmap = GetJson.getBitmap(GetJson.img_url(json.jsonMap().get(cont)));
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap == null) {
Toast.makeText(MainActivity.this, "noImg", Toast.LENGTH_SHORT).show();
}
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bitmap);
}
}
}
2016/11/18
也可以直接加载InputStream的方式
bitmap = BitmapFactory.decodeStream(HttpTool.gis("http://s.nownews.com/media_crop/119027/hash/22/c9/22c9873afe491faf76ee832b96dbfec8.JPG"));
也可以直接加载InputStream的方式
bitmap = BitmapFactory.decodeStream(HttpTool.gis("http://s.nownews.com/media_crop/119027/hash/22/c9/22c9873afe491faf76ee832b96dbfec8.JPG"));
0 件のコメント:
コメントを投稿