从url解析Android中的JSON
我正在制作一个应用程序,其中我必须从url解析以下JSON=”http://www.omdbapi.com/?t=drishyam" JSON是:从url解析Android中的JSON,android,json,parsing,Android,Json,Parsing,我正在制作一个应用程序,其中我必须从url解析以下JSON=”http://www.omdbapi.com/?t=drishyam" JSON是: {"Title":"Drishyam","Year":"2015","Rated":"N/A","Released":"31 Jul 2015","Runtime":"163 min","Genre":"Drama, Mystery, Thriller","Director":"Nishikant Kamat","Writer":"Jeethu Jo
{"Title":"Drishyam","Year":"2015","Rated":"N/A","Released":"31 Jul 2015","Runtime":"163 min","Genre":"Drama, Mystery, Thriller","Director":"Nishikant Kamat","Writer":"Jeethu Joseph (original story), Upendra Sidhaye (adapted by)","Actors":"Ajay Devgn, Shriya Saran, Tabu, Rajat Kapoor","Plot":"Desperate measures are taken by a man who tries to save his family from the dark side of the law, after they commit an unexpected crime.","Language":"Hindi","Country":"India","Awards":"N/A","Poster":"http://ia.media-imdb.com/images/M/MV5BMTYyMjgyNDY3N15BMl5BanBnXkFtZTgwOTMzNTE5NTE@._V1_SX300.jpg","Metascore":"N/A","imdbRating":"8.9","imdbVotes":"16,509","imdbID":"tt4430212","Type":"movie","Response":"True"}
MainActivity.java
private class GetContacts extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Bitmap doInBackground(String... abc) {
html = new Parsing();
html.JsonParse(url);
contactList = new ArrayList<LinkedHashMap<String, String>>();
contactList.clear();
contactList=html.contactList;
System.out.println("Size od cintactlist in Main " + contactList.size());
int c=0;
for (LinkedHashMap<String, String> map : contactList) {
for (Map.Entry<String, String> mapEntry : map.entrySet()) {
if (c == 0) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
if(!key.equals("poster"))
ans="key "+" is "+value+"\n\n";
else
poster=value;
System.out.println("Value of key and value is " + key + " " + value);
} else
break;
}
c++;
}
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(poster).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
// Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
@Override
protected void onPostExecute(Bitmap result) {
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
lbName.setText(ans);
imgview.setImageBitmap(result);
// lblEmail.setText(ans);
}
}
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=新建进度对话框(MainActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护位图doInBackground(字符串…abc){
html=新解析();
JsonParse(url);
contactList=新的ArrayList();
contactList.clear();
contactList=html.contactList;
System.out.println(“Main中的Size od cintactlist”+contactList.Size());
int c=0;
用于(LinkedHashMap映射:联系人列表){
对于(Map.Entry mapEntry:Map.entrySet()){
如果(c==0){
String key=mapEntry.getKey();
字符串值=mapEntry.getValue();
如果(!key.equals(“海报”))
ans=“key”+”是“+value+”\n\n”;
其他的
海报=价值;
System.out.println(“键和值的值为“+键+”+值);
}否则
打破
}
C++;
}
位图mIcon11=null;
试一试{
InputStream in=newjava.net.URL(poster.openStream();
mIcon11=BitmapFactory.decodeStream(in);
}捕获(例外e){
//Log.e(“Error”,e.getMessage());
e、 printStackTrace();
}
返回mIcon11;
}
@凌驾
受保护的void onPostExecute(位图结果){
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
lbName.setText(ans);
imgview.setImageBitmap(结果);
//lblEmail.setText(ans);
}
}
解析.java
package com.movie.comparemovie;
import android.content.Context;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class Parsing {
Context context;
ArrayList<LinkedHashMap<String, String>> contactList;
public void JsonParse(String URL1) {
contactList = new ArrayList<LinkedHashMap<String, String>>();
String logoLink = null;
try {
// instantiate our json parser
JsonParser jParser = new JsonParser();
JSONObject json = jParser.getJSONFromUrl(URL1);
String Title = json.getString("Title");
System.out.println("Title is "+Title);
String tab1_text = json.getString("tab1_text");
int active = json.getInt("active");
System.out.println("Movie Title is :" + Title);
// // Storing each json item in variable
// String year = json1.getString("Year");
// String released = json1.getString("Released");
// String runtime = json1.getString("Runtime");
// String poster = json1.getString("Poster");
LinkedHashMap<String, String> value = new LinkedHashMap<String, String>();
value.put("title", Title);
// value.put("Year", year);
// value.put("Runtime", runtime);
// value.put("Poster", poster);
contactList.add(value);
// System.out.println("Size od cintactlist in Prasing " + contactList.size());
} catch (JSONException e1) {
e1.printStackTrace();
}
}
public class JsonParser {
final String TAG = "JsonParser.java";
InputStream is = null;
JSONObject jObj = null;
String json = "";
public JSONObject getJSONFromUrl(String ul) {
System.out.println("Inside jsonparser class");
try {
URL url = new URL(ul);
URLConnection connection = url.openConnection();
//connection.addRequestProperty("Referer", "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%22mixorg.com%22&rsz=8");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
builder.append(line);
System.out.println(builder.toString());
}
json = builder.toString();
} catch (Exception e) {
Log.e(TAG, "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e(TAG, "Error parsing json data " + e.toString());
} catch (Exception e) {
}
// return JSON String
return jObj;
}
}
}
package com.movie.comparemovie;
导入android.content.Context;
导入android.util.Log;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.URL;
导入java.net.URLConnection;
导入java.util.ArrayList;
导入java.util.LinkedHashMap;
公共类解析{
语境;
ArrayList联系人列表;
public void JsonParse(字符串URL1){
contactList=新的ArrayList();
字符串logoLink=null;
试一试{
//实例化我们的json解析器
JsonParser jParser=新的JsonParser();
JSONObject json=jParser.getJSONFromUrl(URL1);
String Title=json.getString(“Title”);
System.out.println(“标题为”+标题);
String tab1_text=json.getString(“tab1_text”);
int active=json.getInt(“active”);
System.out.println(“电影标题为:“+Title”);
////将每个json项存储在变量中
//字符串year=json1.getString(“年”);
//String released=json1.getString(“released”);
//stringruntime=json1.getString(“runtime”);
//stringposter=json1.getString(“poster”);
LinkedHashMap值=新建LinkedHashMap();
价值。出售(“所有权”,所有权);
//价值。卖出价(“年”,年);
//value.put(“Runtime”,Runtime);
//价值。投入(“海报”,海报);
联系人列表。添加(值);
//System.out.println(“在Prasing中设置的联系人列表大小”+contactList.Size());
}捕获(JSONException e1){
e1.printStackTrace();
}
}
公共类JsonParser{
最后一个字符串TAG=“JsonParser.java”;
InputStream=null;
JSONObject jObj=null;
字符串json=“”;
公共JSONObject getJSONFromUrl(字符串ul){
System.out.println(“内部jsonparser类”);
试一试{
URL=新URL(ul);
URLConnection=url.openConnection();
//connection.addRequestProperty(“Referer”https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%22mixorg.com%22&rsz=8");
弦线;
StringBuilder=新的StringBuilder();
BufferedReader=new BufferedReader(new InputStreamReader(connection.getInputStream());
而((line=reader.readLine())!=null){
builder.append(行);
System.out.println(builder.toString());
}
json=builder.toString();
}捕获(例外e){
Log.e(标记“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(标记“错误解析json数据”+e.toString());
}捕获(例外e){
}
//返回JSON字符串
返回jObj;
}
}
}
其中JsonParser是一个类,getJSONfromURL返回jsonobject。
但它不起作用。
如何解析这个JSON?这样做
public class getData extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading category....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
String response = null;
try {
URL url = new URL("http://www.omdbapi.com/?t=drishyam");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
response = IOUtils.toString(in, "UTF-8");
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
try {
JSONObject c = new JSONObject(result);
String Title = c.getString("Title");
String Year = c.getString("Year");
String Rated = c.getString("Rated");
String Released = c.getString("Released");
String Runtime = c.getString("Runtime");
String Genre = c.getString("Genre");
String Genre = c.getString("Genre");
String Writer = c.getString("Writer");
String Actors = c.getString("Actors");
String Plot = c.getString("Plot");
String Language = c.getString("Language");
String Country = c.getString("Country");
String Awards = c.getString("Awards");
String Poster = c.getString("Poster");
String Metascore = c.getString("Metascore");
String imdbRating = c.getString("imdbRating");
String imdbVotes = c.getString("imdbVotes");
String imdbID = c.getString("imdbID");
String Type = c.getString("Type");
String Response = c.getString("Response");
Log.e("", "TAG : - " + id);
} catch (Exception e) {
Log.e("", "Home Exception : " + e.toString());
}
}
pDialog.dismiss();
getActivity().runOnUiThread(new Runnable() {
public void run() {
ListView.setAdapter(
new MenuAdapter(getActivity(), arrayList, 0));
}
});
}
}
公共类getData扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());
pDialog.setMessage(“加载类别…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护字符串doInBackgroun
try {
JSONObject json = new JSONObject("Your String");
String title = json.getString("Title");
String year = json.getString("Year");
//get your all other keys just like title and year
} catch (JSONException e) {
e.printStackTrace();
}