Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I have to load data from a remote mysql database for my app. The table contains event id, event name and event details. I have to get the last data from the table and show it in 3 text views. I tried a tutorial from androidhive. But it doesn't seem to work. please help.
 

class to retrieve the data
	class GetEventDetails extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(MainActivity.this);
			pDialog.setMessage("Loading data from servers. Please wait...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}
		/**
		 * Getting product details in background thread
		 * */
		protected String doInBackground(String... params) {
 
			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {
					// Check for success tag
					int success;
					try {
						// Building Parameters
						//String pid="1";
						List<NameValuePair> params = new ArrayList<NameValuePair>();
						// getting product details by making HTTP request
						// Note that product details url will use GET request
						JSONObject json = jsonParser.makeHttpRequest(
								url_product_detials, "GET", params);
 
						// check your log for json response
						Log.d("Single Product Details", json.toString());
 
						// json success tag
						success = json.getInt(TAG_SUCCESS);
						if (success == 1) {
							// successfully received product details
							JSONArray eventObj = json
									.getJSONArray(TAG_EVENT); // JSON Array

							// get first product object from JSON Array
							JSONObject event = eventObj.getJSONObject(0);
 
							// product with this pid found
							TextView txtEventname=(TextView) findViewById(R.id.textView2);
							TextView txtEventdet=(TextView) findViewById(R.id.textView3);
							// Edit Text
							txtEventname.setText(event.getString(TAG_EVENTITLE));
							txtEventdet.setText(event.getString(TAG_EVENTDET));
						}else{
							// product with pid not found
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			});
 
			return null;
		}
 

}
 

 
the Jason parsor class
 
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.util.Log;
 
public class JSONParser {
 
	static InputStream is = null;
	static JSONObject jObj = null;
	static String json = "";
 
	// constructor
	public JSONParser() {
 
	}
 
	// function get json from url
	// by making HTTP POST or GET mehtod
	public JSONObject makeHttpRequest(String url, String method,
			List<NameValuePair> params) {
 
		// Making HTTP request
		try {
			
			// check for request method
			if(method == "POST"){
				// request method is POST
				// defaultHttpClient
				DefaultHttpClient httpClient = new DefaultHttpClient();
				HttpPost httpPost = new HttpPost(url);
				httpPost.setEntity(new UrlEncodedFormEntity(params));
 
				HttpResponse httpResponse = httpClient.execute(httpPost);
				HttpEntity httpEntity = httpResponse.getEntity();
				is = httpEntity.getContent();
				
			}else if(method == "GET"){
				// request method is GET
				DefaultHttpClient httpClient = new DefaultHttpClient();
				String paramString = URLEncodedUtils.format(params, "utf-8");
				url += "?" + paramString;
				HttpGet httpGet = new HttpGet(url);
 
				HttpResponse httpResponse = httpClient.execute(httpGet);
				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;
 
	}
}
 

My error is
04-28 15:43:29.918: D/dalvikvm(8036): Late-enabling CheckJNI
04-28 15:43:30.278: D/dalvikvm(8036): GC_FOR_ALLOC freed 78K, 12% free 9560K/10759K, paused 33ms, total 33ms
04-28 15:43:30.288: I/dalvikvm-heap(8036): Grow heap (frag case) to 10.705MB for 437672-byte allocation
04-28 15:43:30.358: D/dalvikvm(8036): GC_CONCURRENT freed <1K, 11% free 9986K/11207K, paused 13ms+7ms, total 70ms
04-28 15:43:30.358: D/dalvikvm(8036): WAIT_FOR_CONCURRENT_GC blocked 55ms
04-28 15:43:30.449: D/dalvikvm(8036): GC_FOR_ALLOC freed 2K, 11% free 10211K/11463K, paused 56ms, total 57ms
04-28 15:43:30.649: D/libEGL(8036): loaded /system/lib/egl/libEGL_mali.so
04-28 15:43:30.759: D/libEGL(8036): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-28 15:43:30.789: D/libEGL(8036): loaded /system/lib/egl/libGLESv2_mali.so
04-28 15:43:30.879: D/OpenGLRenderer(8036): Enabling debug mode 0
04-28 15:43:32.130: D/dalvikvm(8036): GC_CONCURRENT freed 11K, 9% free 10906K/11975K, paused 4ms+6ms, total 56ms
04-28 15:43:32.130: D/dalvikvm(8036): WAIT_FOR_CONCURRENT_GC blocked 31ms
04-28 15:43:32.200: D/dalvikvm(8036): GC_FOR_ALLOC freed <1K, 10% free 11335K/12487K, paused 47ms, total 47ms
04-28 15:43:32.290: D/dalvikvm(8036): GC_CONCURRENT freed 3K, 8% free 11992K/12999K, paused 3ms+13ms, total 71ms
04-28 15:43:32.681: E/SpannableStringBuilder(8036): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-28 15:43:32.681: E/SpannableStringBuilder(8036): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-28 15:43:34.422: E/SpannableStringBuilder(8036): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-28 15:43:34.422: E/SpannableStringBuilder(8036): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-28 15:43:35.994: D/ProgressBar(8036): setProgress = 0
04-28 15:43:35.994: D/ProgressBar(8036): setProgress = 0, fromUser = false
04-28 15:43:35.994: D/ProgressBar(8036): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
04-28 15:43:36.124: W/ResourceType(8036): Failure getting entry for 0x010802c8 (t=7 e=712) in package 0 (error -75)
04-28 15:43:36.474: D/AndroidRuntime(8036): Shutting down VM
04-28 15:43:36.474: W/dalvikvm(8036): threadid=1: thread exiting with uncaught exception (group=0x41a162a0)
04-28 15:43:36.534: E/AndroidRuntime(8036): FATAL EXCEPTION: main
04-28 15:43:36.534: E/AndroidRuntime(8036): android.os.NetworkOnMainThreadException
04-28 15:43:36.534: E/AndroidRuntime(8036):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at com.mahavega.qcdemo.JSONParser.makeHttpRequest(JSONParser.java:62)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at com.mahavega.qcdemo.MainActivity$GetEventDetails$1.run(MainActivity.java:198)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at android.os.Handler.handleCallback(Handler.java:615)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at android.os.Looper.loop(Looper.java:137)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at android.app.ActivityThread.main(ActivityThread.java:4856)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at java.lang.reflect.Method.invoke(Method.java:511)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
04-28 15:43:36.534: E/AndroidRuntime(8036):     at dalvik.system.NativeStart.main(Native Method)
Posted 27-Apr-13 21:16pm
Edited 30-Apr-13 23:40pm
v3
Comments
Sandeep Mewara at 28-Apr-13 3:12am
   
But it doesn't seem to work
It would be good if you share what didn't work. Currently, it's an open question that does not share where are you stuck and what help you need.
Sumodh Krishna at 28-Apr-13 3:31am
   
updated the question
Prasad Khandekar at 28-Apr-13 11:01am
   
Still you have not explained what does not work. Are you getting any errors?
Sumodh Krishna at 28-Apr-13 12:39pm
   
It shows some error in logs.. when I try to run the function the app stops
Sudhakar Shinde at 30-Apr-13 8:42am
   
There will be some solution for the errors if you want to share with us. Without seeing the errors noone can provide you the solution.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

just add in oncreate the following code
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		  .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
		  .penaltyLog().build());
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 4 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100