json_parsing & web view gujinfo
//aama click kreye url opern tay ie webview lidhi che
Step 1:main_activity xml & java
Step 2:simple.xml
Step 3:Heo.java(pojo)
Step 4:jsonadapter.java
Step 5:jsonclass.java
Step 6:webview_xml &java
Step 1:main_activity xml & java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.web.MainActivity">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.jay.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"></ListView>
</LinearLayout>
</LinearLayout>
Step 1:MainActivity.java
package demoproject.aalap.com.web;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView lv;
String URL_GATE_DATA="https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=c28754a20455455995a923e3c309ba06",result;
ArrayList<Hero> arrayList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lv);
select sel=new select();//innter class take
sel.execute();
}
private class select extends AsyncTask {
ProgressDialog pg;
jsonclass jc;
@Override
protected void onPreExecute() {
super.onPreExecute();
pg=new ProgressDialog(MainActivity.this);
pg.setTitle("Fetching....");
pg.setMessage("wait");
pg.setCancelable(true);
pg.setIndeterminate(false);
pg.show();
}
@Override
protected Object doInBackground(Object[] objects) {
jc=new jsonclass();
result=jc.processurl(URL_GATE_DATA);
try {
JSONObject jsonObject=new JSONObject(result);
JSONArray articles=jsonObject.getJSONArray("articles");
for (int i = 0; i < articles.length(); i++) {
JSONObject a=articles.getJSONObject(i);
Hero h=new Hero();
h.setAuthor(a.getString("author"));
h.setTitle(a.getString("title"));
h.setUrlToImage(a.getString("urlToImage"));
h.setPublishedAt(a.getString("publishedAt"));
h.setDescription(a.getString("description"));
h.setUrl(a.getString("url"));
arrayList.add(h);
}
// lv.setAdapter(new ImageAdapter(this));
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
if(pg.isShowing()){
pg.dismiss();
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// String url = MainActivity.get(i).get("url").toString();
Intent intent = new Intent(MainActivity.this,webview.class);
intent.putExtra("web",arrayList.get(i).getUrl());
startActivity(intent);
}
});
jsonAdapter jsonadapter=new jsonAdapter(MainActivity.this,arrayList);
lv.setAdapter(jsonadapter);
}
}
}
Step 2:Simple.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">
<ImageView
android:id="@+id/iv"
android:background="@mipmap/ic_launcher"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/tv"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Step 3:Hero.java(pojo)
package demoproject.aalap.com.web;
/**
* Created by ADMIN on 22-07-2018.
*/
public class Hero {
private String status ;
private String totalResults;
private String source;
private String id;
private String name;
private String title;
private String description;
private String author;
private String urlToImage;
private String publishedAt;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private String url;
public String getStatus() {
return status;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getUrlToImage() {
return urlToImage;
}
public void setUrlToImage(String urlToImage) {
this.urlToImage = urlToImage;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setStatus(String status) {
this.status = status;
}
public String getTotalResults() {
return totalResults;
}
public void setTotalResults(String totalResults) {
this.totalResults = totalResults;
}
// public String getArticles() {
// return articles;
//}
//public void setArticles(String articles) {
// this.articles = articles;
//}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 4:jsonAdapter.java
package demoproject.aalap.com.web;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by ADMIN on 22-07-2018.
*/
class jsonAdapter extends BaseAdapter {
Context context;
ArrayList<Hero> arryList=new ArrayList<>();
public jsonAdapter(Context context, ArrayList<Hero> arryList) {
this.context = context;
this.arryList = arryList;
}
@Override
public int getCount() {
return arryList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.simple,null);
ImageView imageView;
TextView textView;
imageView=v.findViewById(R.id.iv);
textView=v.findViewById(R.id.tv);
Hero h=arryList.get(i);
textView.setText(h.getStatus() +"\n"+ h.getTotalResults() +"\n"+ h.getSource() +"\n"+h.getId() +"\n"+h.getName()+"\n"+
h.getAuthor() +"\n"+ h.getTitle() +"\n"+ h.getDescription() +"\n"+h.getPublishedAt()+"\n"+h.getUrlToImage()+"\n"+h.getUrl());
Picasso.get().load(h.getUrlToImage()).into(imageView);
return v;
}
}
Step 5:Jsonclass.java
package demoproject.aalap.com.web;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by ADMIN on 22-07-2018.
*/
public class jsonclass {
String result;
public String processurl(String s)
{
try {
URL url=new URL(s);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code=conn.getResponseCode();
if (code==200)
{
InputStream in=new BufferedInputStream(conn.getInputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line="";
while ((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
result=sb.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
Step 6:webView_xml & java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demoproject.aalap.com.web.webview">
<TextView android:text="WebView" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Step 6:web.java
package demoproject.aalap.com.web;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class webview extends AppCompatActivity {
// Button b1;
//EditText ed1;
private WebView wv1;
String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
String data = getIntent().getExtras().getString("web");
// b1=(Button)findViewById(R.id.button);
//ed1=(EditText)findViewById(R.id.editText);
wv1=(WebView)findViewById(R.id.webView);
wv1.setWebViewClient(new MyBrowser());
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(data);
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Step 1:main_activity xml & java
Step 2:simple.xml
Step 3:Heo.java(pojo)
Step 4:jsonadapter.java
Step 5:jsonclass.java
Step 6:webview_xml &java
Step 1:main_activity xml & java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.web.MainActivity">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.jay.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"></ListView>
</LinearLayout>
</LinearLayout>
Step 1:MainActivity.java
package demoproject.aalap.com.web;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView lv;
String URL_GATE_DATA="https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=c28754a20455455995a923e3c309ba06",result;
ArrayList<Hero> arrayList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=findViewById(R.id.lv);
select sel=new select();//innter class take
sel.execute();
}
private class select extends AsyncTask {
ProgressDialog pg;
jsonclass jc;
@Override
protected void onPreExecute() {
super.onPreExecute();
pg=new ProgressDialog(MainActivity.this);
pg.setTitle("Fetching....");
pg.setMessage("wait");
pg.setCancelable(true);
pg.setIndeterminate(false);
pg.show();
}
@Override
protected Object doInBackground(Object[] objects) {
jc=new jsonclass();
result=jc.processurl(URL_GATE_DATA);
try {
JSONObject jsonObject=new JSONObject(result);
JSONArray articles=jsonObject.getJSONArray("articles");
for (int i = 0; i < articles.length(); i++) {
JSONObject a=articles.getJSONObject(i);
Hero h=new Hero();
h.setAuthor(a.getString("author"));
h.setTitle(a.getString("title"));
h.setUrlToImage(a.getString("urlToImage"));
h.setPublishedAt(a.getString("publishedAt"));
h.setDescription(a.getString("description"));
h.setUrl(a.getString("url"));
arrayList.add(h);
}
// lv.setAdapter(new ImageAdapter(this));
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
if(pg.isShowing()){
pg.dismiss();
}
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// String url = MainActivity.get(i).get("url").toString();
Intent intent = new Intent(MainActivity.this,webview.class);
intent.putExtra("web",arrayList.get(i).getUrl());
startActivity(intent);
}
});
jsonAdapter jsonadapter=new jsonAdapter(MainActivity.this,arrayList);
lv.setAdapter(jsonadapter);
}
}
}
Step 2:Simple.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">
<ImageView
android:id="@+id/iv"
android:background="@mipmap/ic_launcher"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/tv"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Step 3:Hero.java(pojo)
package demoproject.aalap.com.web;
/**
* Created by ADMIN on 22-07-2018.
*/
public class Hero {
private String status ;
private String totalResults;
private String source;
private String id;
private String name;
private String title;
private String description;
private String author;
private String urlToImage;
private String publishedAt;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private String url;
public String getStatus() {
return status;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getUrlToImage() {
return urlToImage;
}
public void setUrlToImage(String urlToImage) {
this.urlToImage = urlToImage;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setStatus(String status) {
this.status = status;
}
public String getTotalResults() {
return totalResults;
}
public void setTotalResults(String totalResults) {
this.totalResults = totalResults;
}
// public String getArticles() {
// return articles;
//}
//public void setArticles(String articles) {
// this.articles = articles;
//}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 4:jsonAdapter.java
package demoproject.aalap.com.web;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
* Created by ADMIN on 22-07-2018.
*/
class jsonAdapter extends BaseAdapter {
Context context;
ArrayList<Hero> arryList=new ArrayList<>();
public jsonAdapter(Context context, ArrayList<Hero> arryList) {
this.context = context;
this.arryList = arryList;
}
@Override
public int getCount() {
return arryList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.simple,null);
ImageView imageView;
TextView textView;
imageView=v.findViewById(R.id.iv);
textView=v.findViewById(R.id.tv);
Hero h=arryList.get(i);
textView.setText(h.getStatus() +"\n"+ h.getTotalResults() +"\n"+ h.getSource() +"\n"+h.getId() +"\n"+h.getName()+"\n"+
h.getAuthor() +"\n"+ h.getTitle() +"\n"+ h.getDescription() +"\n"+h.getPublishedAt()+"\n"+h.getUrlToImage()+"\n"+h.getUrl());
Picasso.get().load(h.getUrlToImage()).into(imageView);
return v;
}
}
Step 5:Jsonclass.java
package demoproject.aalap.com.web;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by ADMIN on 22-07-2018.
*/
public class jsonclass {
String result;
public String processurl(String s)
{
try {
URL url=new URL(s);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code=conn.getResponseCode();
if (code==200)
{
InputStream in=new BufferedInputStream(conn.getInputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line="";
while ((line=br.readLine())!=null)
{
sb.append(line+"\n");
}
result=sb.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
Step 6:webView_xml & java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demoproject.aalap.com.web.webview">
<TextView android:text="WebView" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Step 6:web.java
package demoproject.aalap.com.web;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class webview extends AppCompatActivity {
// Button b1;
//EditText ed1;
private WebView wv1;
String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
String data = getIntent().getExtras().getString("web");
// b1=(Button)findViewById(R.id.button);
//ed1=(EditText)findViewById(R.id.editText);
wv1=(WebView)findViewById(R.id.webView);
wv1.setWebViewClient(new MyBrowser());
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(data);
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Comments
Post a Comment