Reputation: 1615
I'm very new to Android environment. I was wandering about retrieving JSON content from a web page, so I've found this tutorial to get JSON content from a url (Tutorial Link) and It works properly also with my data.
Now, what I wanted to do: a Tab application, with one of these tabs using that code to retrieve JSON data. I've used fragment and I tried to modify code in order to be compatible with fragments, anyway as I start the related tab, the application crashes.
As the previous tutorial works properly, I'm pretty sure the problem is on my newbie code.
FragmentTab1.Java
package com.alessandrobocci.fenice;
import java.util.ArrayList;
import java.util.HashMap;
import com.alessandrobocci.fenice.library.JSONParser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class FragmentTab1 extends Fragment {
ListView list;
TextView title;
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://10.0.2.2/test/read.php";
//JSON Node Names
private static final String TAG_OS = "todo";
private static final String TAG_VER = "title";
JSONArray arr = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflate the layout for this fragment
//This layout contains your list view
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
title = (TextView)getView().findViewById(R.id.vers);
//now you must initialize your list view
list =(ListView)view.findViewById(R.id.listnews);
new JSONParse().execute();
return view;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
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
arr = json.getJSONArray(TAG_OS);
for(int i = 0; i < arr.length(); i++){
JSONObject c = arr.getJSONObject(i);
// Storing JSON item in a Variable
String ver = c.getString(TAG_VER);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_VER, ver);
oslist.add(map);
//list=(ListView) getView().findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.list_v,
new String[] { TAG_VER }, new int[] {
R.id.vers});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "You Clicked at "+oslist.get(+position).get("title"), Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
logCat:
07-04 14:59:36.250: D/gralloc_goldfish(1081): Emulator without GPU emulation detected.
07-04 14:59:38.350: D/dalvikvm(1081): GC_CONCURRENT freed 154K, 10% free 6030K/6663K, paused 11ms+8ms
07-04 14:59:38.690: W/System.err(1081): org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2 refused
07-04 14:59:38.741: W/System.err(1081): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
07-04 14:59:38.741: W/System.err(1081): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-04 14:59:38.881: W/System.err(1081): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-04 14:59:38.910: W/System.err(1081): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-04 14:59:38.910: W/System.err(1081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-04 14:59:39.100: W/System.err(1081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-04 14:59:39.150: W/System.err(1081): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-04 14:59:39.150: W/System.err(1081): at com.alessandrobocci.fenice.library.JSONParser.getJSONFromUrl(JSONParser.java:38)
07-04 14:59:39.200: W/System.err(1081): at com.alessandrobocci.fenice.FragmentTab1$JSONParse.doInBackground(FragmentTab1.java:72)
07-04 14:59:39.310: W/System.err(1081): at com.alessandrobocci.fenice.FragmentTab1$JSONParse.doInBackground(FragmentTab1.java:1)
07-04 14:59:39.330: W/System.err(1081): at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-04 14:59:39.330: W/System.err(1081): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-04 14:59:39.390: W/System.err(1081): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-04 14:59:39.440: W/System.err(1081): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-04 14:59:39.440: W/System.err(1081): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-04 14:59:39.500: W/System.err(1081): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-04 14:59:39.510: W/System.err(1081): at java.lang.Thread.run(Thread.java:856)
07-04 14:59:39.510: W/System.err(1081): Caused by: java.net.ConnectException: socket failed: EACCES (Permission denied)
07-04 14:59:39.611: W/System.err(1081): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:181)
07-04 14:59:39.710: W/System.err(1081): ... 16 more
07-04 14:59:39.710: W/System.err(1081): Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
07-04 14:59:39.870: W/System.err(1081): at libcore.io.IoBridge.socket(IoBridge.java:573)
07-04 14:59:39.870: W/System.err(1081): at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
07-04 14:59:39.870: W/System.err(1081): at java.net.Socket.checkOpenAndCreate(Socket.java:663)
07-04 14:59:39.921: W/System.err(1081): at java.net.Socket.connect(Socket.java:807)
07-04 14:59:39.979: W/System.err(1081): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-04 14:59:39.979: W/System.err(1081): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-04 14:59:40.051: W/System.err(1081): ... 16 more
07-04 14:59:40.091: W/System.err(1081): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
07-04 14:59:40.210: W/System.err(1081): at libcore.io.Posix.socket(Native Method)
07-04 14:59:40.240: W/System.err(1081): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:169)
07-04 14:59:40.260: W/System.err(1081): at libcore.io.IoBridge.socket(IoBridge.java:558)
07-04 14:59:40.310: W/System.err(1081): ... 21 more
07-04 14:59:40.370: E/Buffer Error(1081): Error converting result java.lang.NullPointerException
07-04 14:59:40.380: E/JSON Parser(1081): Error parsing data org.json.JSONException: End of input at character 0 of
07-04 14:59:40.390: D/AndroidRuntime(1081): Shutting down VM
07-04 14:59:40.403: W/dalvikvm(1081): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
07-04 14:59:40.452: E/AndroidRuntime(1081): FATAL EXCEPTION: main
07-04 14:59:40.452: E/AndroidRuntime(1081): java.lang.NullPointerException
07-04 14:59:40.452: E/AndroidRuntime(1081): at com.alessandrobocci.fenice.FragmentTab1$JSONParse.onPostExecute(FragmentTab1.java:80)
07-04 14:59:40.452: E/AndroidRuntime(1081): at com.alessandrobocci.fenice.FragmentTab1$JSONParse.onPostExecute(FragmentTab1.java:1)
07-04 14:59:40.452: E/AndroidRuntime(1081): at android.os.AsyncTask.finish(AsyncTask.java:602)
07-04 14:59:40.452: E/AndroidRuntime(1081): at android.os.AsyncTask.access$600(AsyncTask.java:156)
07-04 14:59:40.452: E/AndroidRuntime(1081): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
07-04 14:59:40.452: E/AndroidRuntime(1081): at android.os.Handler.dispatchMessage(Handler.java:99)
07-04 14:59:40.452: E/AndroidRuntime(1081): at android.os.Looper.loop(Looper.java:137)
07-04 14:59:40.452: E/AndroidRuntime(1081): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-04 14:59:40.452: E/AndroidRuntime(1081): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 14:59:40.452: E/AndroidRuntime(1081): at java.lang.reflect.Method.invoke(Method.java:511)
07-04 14:59:40.452: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-04 14:59:40.452: E/AndroidRuntime(1081): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-04 14:59:40.452: E/AndroidRuntime(1081): at dalvik.system.NativeStart.main(Native Method)
If necessary, I'll post also the other code, I don't think anyway it's useful, as for example MainActivity.Java (who contains Tabs) works with simple FragmenTab code that retrieves some text. Hope I was clear, thanks.
Upvotes: 0
Views: 1821
Reputation: 133560
Change this
title = (TextView)getView().findViewById(R.id.vers);
to
title = (TextView)view.findViewById(R.id.vers);
title
is probably null leading to NullPointerException
which can be confirmed if you post the stacktrace.
One more tip you have the adapter set in the for loop. No need to do that. Add items to list and set adapter to listview once
Upvotes: 1